13.1 QQ邮箱

news/2024/5/18 19:15:56

1. 邮箱发送

在这里插入图片描述

2. 准备工作

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3. 整合SpringBoot

3.1 配置

依赖引入

        <!--        邮件服务--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>

application.yml

spring:mail:host: smtp.qq.comport: 587username: QQ邮箱password: 授权码default-encoding: UTF-8properties:mail:debug: truesmtp:sockFactory:class: javax.net.ssl.SSLSocketFactory

在这里插入图片描述
在这里插入图片描述

其他

邮件对象
package com.ruoyi.common.vo;import lombok.Data;/*** 邮件对象*/
@Data
public class MailMessage {//发送者private String from;//接受者private String to;//抄送人private String cc;//主题private String subject;//内容private String text;//附件private MultipartFile file;
}

3.2 发送简单邮件

创建邮箱组件

package com.ruoyi.common.component;import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.vo.MailMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;import javax.annotation.Resource;/*** qq邮箱*/
@Component
public class MailService {@Value("${spring.mail.username}")private String mailFrom;@Resourceprivate JavaMailSender javaMailSender;/*** 发送邮件** @param from    发送者* @param to      收件人* @param cc      抄送人* @param subject 主题* @param content 内容*/public void sendSimpleMail(String from, String to, String cc, String subject, String content) {SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setFrom(mailFrom);mailMessage.setTo(to);mailMessage.setCc(cc);mailMessage.setSubject(subject);mailMessage.setText(content);javaMailSender.send(mailMessage);}public void sendSimpleMail(MailMessage message) {SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setFrom(mailFrom);String[] tos = StrUtil.splitToArray(message.getTo(), ";");mailMessage.setTo(tos);String[] ccs = StrUtil.splitToArray(message.getCc(), ";");mailMessage.setCc(ccs);mailMessage.setSubject(message.getSubject());mailMessage.setText(message.getText());javaMailSender.send(mailMessage);}
}

在这里插入图片描述

测试

控制层

    @Resourceprivate MailService mailService;/*** 发送邮件** @param user* @return*/@GetMapping("/mail")public AjaxResult mail(MailMessage mailMessage) {mailService.sendSimpleMail(mailMessage);return AjaxResult.success();}

在这里插入图片描述

在这里插入图片描述

3.3 发送带附件的邮件

邮箱组件

在这里插入图片描述

package com.ruoyi.common.component;import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.vo.MailMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;/*** qq邮箱*/
@Component
public class MailService {@Value("${spring.mail.username}")private String mailFrom;@Resourceprivate JavaMailSender javaMailSender;/*** 发送带附件的邮件** @param message* @param file*/public void sendAttachFileMail(MailMessage message, File file) {try {MimeMessage mimeMessage = javaMailSender.createMimeMessage();MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);mimeMessageHelper.setFrom(mailFrom);String[] tos = StrUtil.splitToArray(message.getTo(), ";");mimeMessageHelper.setTo(tos);String[] ccs = StrUtil.splitToArray(message.getCc(), ";");mimeMessageHelper.setCc(ccs);mimeMessageHelper.setSubject(message.getSubject());mimeMessageHelper.setText(message.getText());mimeMessageHelper.addAttachment(file.getName(), file);javaMailSender.send(mimeMessage);} catch (MessagingException e) {throw new RuntimeException(e);}}
}

在这里插入图片描述

使用及测试

    @Resourceprivate MailService mailService;/*** 发送邮件带附件** @param mailMessage* @return*/@PostMapping("/mailFile")public AjaxResult mailFile(MailMessage mailMessage, MultipartFile multipartFile) {try {// 获取文件名String fileName = multipartFile.getOriginalFilename();// 获取文件后缀(.xml)String suffix = fileName.substring(fileName.lastIndexOf("."));File file = File.createTempFile(fileName.substring(0, fileName.lastIndexOf(".")), suffix);multipartFile.transferTo(file);mailService.sendAttachFileMail(mailMessage, file);} catch (IOException e) {throw new RuntimeException(e);}return AjaxResult.success();}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.4 发送带图片资源的邮件

在这里插入图片描述

组件

package com.ruoyi.common.component;import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.vo.MailMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.web.util.HtmlUtils;import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;/*** qq邮箱*/
@Component
public class MailService {@Value("${spring.mail.username}")private String mailFrom;@Resourceprivate JavaMailSender javaMailSender;/*** 发送正文带图片的邮件** @param message*/public void sendMailWithirng(MailMessage message) {String[] srcPath = message.getSrcPath().split(",");String[] resIds = message.getResIds().split(",");if (srcPath.length != resIds.length) {throw new ServiceException("图片信息不全,发送失败!");}try {MimeMessage mimeMessage = javaMailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);helper.setFrom(mailFrom);String[] tos = StrUtil.splitToArray(message.getTo(), ";");helper.setTo(tos);String[] ccs = StrUtil.splitToArray(message.getCc(), ";");helper.setCc(ccs);helper.setSubject(message.getSubject());helper.setText(message.getText(), true);for (int i = 0; i < srcPath.length; i++) {FileSystemResource fileSystemResource = new FileSystemResource(new File(srcPath[i]));helper.addInline(resIds[i], fileSystemResource);}javaMailSender.send(mimeMessage);} catch (MessagingException e) {throw new RuntimeException(e);}}
}

在这里插入图片描述

测试

    @Resourceprivate MailService mailService;/*** 发送正文带图片的邮件** @param mailMessage* @return*/@PostMapping("/sendMailWithirng")public AjaxResult sendMailWithirng(@Validated @RequestBody MailMessage mailMessage) {mailService.sendMailWithirng(mailMessage);return AjaxResult.success();}

3.5 使用 FreeMarker 构建邮件模报

在这里插入图片描述

依赖

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency>

组件

package com.ruoyi.common.component;import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.vo.MailMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.web.util.HtmlUtils;import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;/*** qq邮箱*/
@Component
public class MailService {@Value("${spring.mail.username}")private String mailFrom;@Resourceprivate JavaMailSender javaMailSender;/*** 使用 FreeMarker 构建邮件模报** @param message*/public void sendHtmlMail(MailMessage message) {try {MimeMessage mimeMessage = javaMailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);helper.setFrom(mailFrom);String[] tos = StrUtil.splitToArray(message.getTo(), ";");helper.setTo(tos);String[] ccs = StrUtil.splitToArray(message.getCc(), ";");helper.setCc(ccs);helper.setSubject(message.getSubject());helper.setText(message.getText(), true);javaMailSender.send(mimeMessage);} catch (MessagingException e) {throw new RuntimeException(e);}}
}

邮件模板

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.6 使用 Thymeleaf 构建邮件模板

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

*************************************************


http://www.mrgr.cn/p/31034504

相关文章

部署YUM仓库及NFS共享服务

目录1.YUM仓库服务(1)部署YUM软件仓库(1)准备安装源(2)构建CentOs7软件仓库(3)在软件仓库中加入非官方RPM包组(3)如何搭建本地源仓库、ftp源仓库?2.NFS共享存储服务(1)使用NFS发布共享资源 1.YUM仓库服务 (1)部署YUM软件仓库 YUM 基于RPM包构建的软件更新机制 可…

GLASGOW SMILE: 1.1

靶机描述 靶机地址:https://download.vulnhub.com/glasgowsmile/GlasgowSmile-v1.1.zipDescription Title: Glasgow SmileUsers: 5 Difficulty Level: Initial Shell (Easy) - Privileges Escalation (Intermediate) Hint: Enumeration is the key.If you are a newbie in Pen…

Linux网络-Yum仓库

Yum仓库 1.yum概述 Yum (Yellow dog Updater, Modified) 是一个基于rpm包构建的软件更新机制,能够自动解决软件包之间的依赖关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装。节省了日常工作中的大量查找安装依赖包的时间。 2.yum工作原理 yum依赖于环境,…

[NeurIPS-23] GOHA: Generalizable One-shot 3D Neural Head Avatar

[pdf | proj | code] 本文提出一种基于单图的可驱动虚拟人像重建框架。基于3DMM给粗重建、驱动结果&#xff0c;基于神经辐射场给细粒度平滑结果。 方法 给定源图片I_s和目标图片I_t&#xff0c;希望生成图片I_o具有源图片ID和目标图片表情位姿。本文提出三个分支&#xff1a;…

DarkHole: 2

靶机描述 靶机地址:https://download.vulnhub.com/darkhole/darkhole_2.zipDescription Difficulty:Hard This works better with VMware rather than VirtualBox Hint: Dont waste your time For Brute-Force信息收集 主机发现 利用arp-scan -l命令扫描靶机IP arp-scan -l开放…

Hackable: III

靶机描述 靶机地址:https://download.vulnhub.com/hackable/hackable3.ovaDescription Focus on general concepts about CTF Difficulty: Medium This works better with VirtualBox rather than VMware.信息收集 nmap探测详细端口信息 nmap -sS -sV -A -p- 192.168.56.106探…

Hackable: II

靶机描述 靶机地址:https://download.vulnhub.com/hackable/hackableII.ovaDescription Difficulty: easy This works better with VirtualBox rather than VMware信息收集 端口扫描 通过nmap扫描目标主机发现开放了3个端口 nmap -A -p 1-65535 192.168.56.101FTP发现Web发现 …

开源博客项目Blog .NET Core源码学习(20:App.Hosting项目结构分析-8)

本文学习并分析App.Hosting项目中后台管理页面的个人资料页面、修改密码页面。 个人资料页面 个人资料页面用于显示和编辑个人信息&#xff0c;支持从本地上传个人头像。整个页面使用了layui中的表单、日期与时间选择、上传等样式或模块&#xff0c;通过layui.css文件设置样式…

CORROSION: 1

靶机描述 靶机地址:https://www.vulnhub.com/entry/corrosion-1,730/Description Difficulty: Easy A easy box for beginners, but not too easy. Good Luck. Hint: Enumerate Property.信息收集 利用arp-scan -l命令扫描靶机IP arp-scan -l端口扫描 nmap -sS -p 1-65535 -sV…

React Router、And、Redux动态菜单和动态路由

前言 动态菜单和动态路由的逻辑在登录完成之后,用useEffect监听dispatch把菜单和路由的数据初始化,渲染菜单,redux将路由的静态资源修改。数据结构后端数据符合前端需要的数据结构即可,mock后端接口返回数据 import Mock from mockjs;Mock.mock(/api, get, {code: 200,data…

微服务之SpringCloud AlibabaSeata处理分布式事务

一、概述 1.1背景 一次业务操作需要跨多个数据源或需要跨多个系统进行远程调用&#xff0c;就会产生分布式事务问题 but 关系型数据库提供的能力是基于单机事务的&#xff0c;一旦遇到分布式事务场景&#xff0c;就需要通过更多其他技术手段来解决问题。 全局事务&#xff1a;…

WPF 高仿360功能界面

WPF 高仿360功能界面,如图:XAML TabControl 控件<TabControl Style="{StaticResource TabControlStyle}"><TabItem Header="资源共享" Style="{StaticResource TabItemStyle}"><TabItem.Background><ImageBrush ImageSou…

Django后台项目开发实战五

完成两个功能&#xff1a; HR 可以维护候选人信息面试官可以录入面试反馈 第五阶段 创建 interview 应用&#xff0c;实现候选人面试评估表的增删改功能&#xff0c;并且按照页面分组来展示不同的内容&#xff0c;如候选人基础信息&#xff0c;一面&#xff0c;二面的面试结…

软件设计师:操作系统知识

操作系统层次进程管理 顺序执行真题标记并发执行

软考备考

p5,https://www.bilibili.com/video/BV1Qc411G7fB?p=6&vd_source=1a563cd2b3f3fdeb2a16cbbf18022d2f第一章 计算机组成原理与体系结构基础知识(6) 信息化世界是由计算机/手机通过计算机网络与其他的计算机/手机连接的,其中,计算机/手机由三部分组成,从底层到上层分别…

Mybatis-Plus扩展接口InnerInterceptor

InnerInterceptor 接口就是 MyBatis-Plus 提供的一个拦截器接口&#xff0c;用于实现一些常用的 SQL 处理逻辑&#xff0c;处理 MyBatis-Plus 的特定功能,例如PaginationInnerInterceptor、OptimisticLockerInnerInterceptor 等,都实现了 InnerInterceptor 接口&#xff0c;并添…

【Python基础】进程

文章目录 [toc]程序与进程的区别与联系同步任务示例 并行任务示例进程调度的“随机性” 进程属性与方法process_object.start()方法process_object.join()方法process_object.daemon属性没有设置守护进程的情况设置守护进程的情况 process_object.current_process()方法 进程通…

力扣例题(接雨水)

链接&#xff1a; . - 力扣&#xff08;LeetCode&#xff09; 题目描述&#xff1a; 思路&#xff1a; 判断一块地方是否可以接到雨水&#xff0c;只需要判断他是否有左右边界使他可以接到水 左右边界分别为此处左侧的最高点和右侧的最高点 同时此处可接雨水的高度为左右两…

UE5 蓝图入门

基础节点创建&#xff1a; 常量&#xff1a; 按住 1 &#xff0c;点击鼠标左键&#xff0c;创建常量 二维向量&#xff1a; 按住 2 &#xff0c;点击鼠标左键&#xff0c;创建二维向量 三维向量&#xff1a; 按住 3 &#xff0c;点击鼠标左键 乘法&#xff1a; 按住 m 键…

单调队列:从一道题说起_2023CCPC广东省赛B.基站建设

今天遇到一道题: 给定长度为n的数组a,a[i]表示在第i点建立基站的开销。 同时给出m个区间[li,ri],要满足给定的m个区间内都至少有一个基站,求最小的开销。正解是单调队列优化dp,那么什么是单调队列?我们先看另外一道题:显然最小值和最大值是互相独立的,我们可以先考虑最…