Spring Boot整合邮件配置
Spring Boot 整合邮件配置
概述
这个技术是做什么?学习该技术的原因,技术的难点在哪里。
这个技术能使项目具备发送邮件的功能,这个技术我是作为技术储备来学习的,没想到在学习后没多久就能够有用武之地。该项技术总体难度不大,硬要说难的地方就在于整合模板引擎发送模板邮件,因为还要同时了解一些模板引擎的知识,不过如果有 JSP 相关知识会容易应付得多。
整合邮件发送功能
Spring Boot 2.x 集成了 mail 模块
在 pom.xml 中引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
邮箱配置
一些必要的名词解释
-
什么是 POP3、SMTP 和 IMAP?
他们是三种邮件协议。简单来说,POP3 和 IMAP 是用来从服务器上下载邮件的。SMTP 适用于发送或中转信件时找到下一个目的地。所以我们发送邮件应该使用 SMTP 协议。 -
什么是邮箱客户端授权码?
邮箱客户端授权码是为了避免邮箱密码被盗后,盗号者通过客户端登录邮箱而设计的安防功能。
QQ 邮箱配置
网页登录 QQ 邮箱→设置→开启相应服务并生成授权码
spring boot 配置:
spring:
mail:
host: smtp.qq.com #发送邮件服务器
username: xx@qq.com #QQ 邮箱
password: xxxxxxxxxxx #客户端授权码
protocol: smtp #发送邮件协议
properties.mail.smtp.auth: true
properties.mail.smtp.port: 465 #端口号 465 或 587
properties.mail.display.sendmail: aaa #可以任意
properties.mail.display.sendname: bbb #可以任意
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true #开启 SSL
default-encoding: utf-8
网易系 (126/163/yeah) 邮箱配置
网页登录网易邮箱→设置→POP3/SMTP/IMAP
spring boot 配置:
spring:
mail:
host: smtp.126.com #发送邮件服务器
username: xx@126.com #网易邮箱
password: xxxxxxxx #客户端授权码
protocol: smtp #发送邮件协议
properties.mail.smtp.auth: true
properties.mail.smtp.port: 994 #465 或者 994
properties.mail.display.sendmail: aaa #可以任意
properties.mail.display.sendname: bbb #可以任意
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true #开启 SSL
default-encoding: utf-8
from: xx@126.com
- 126 邮箱 SMTP 服务器地址:smtp.126.com
- 163 邮箱 SMTP 服务器地址:smtp.163.com
- yeah 邮箱 SMTP 服务器地址:smtp.yeah.net
发送简单的文本邮件
写个邮件服务 Service
@Service
public class MailService {
// Spring 官方提供的集成邮件服务的实现类,目前是 Java 后端发送邮件和集成邮件服务的主流工具。
@Resource
private JavaMailSender mailSender;
<span class="hljs-comment">// 从配置文件中注入发件人的姓名</span>
<span class="hljs-meta">@Value("${spring.mail.username}")</span>
<span class="hljs-keyword">private</span> String fromEmail;
<span class="hljs-comment">/**
* 发送文本邮件
*
* <span class="hljs-doctag">@param</span> to 收件人
* <span class="hljs-doctag">@param</span> subject 标题
* <span class="hljs-doctag">@param</span> content 正文
* <span class="hljs-doctag">@throws</span> MessagingException
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">sendSimpleMail</span><span class="hljs-params">(String to, String subject, String content)</span> {
<span class="hljs-type">SimpleMailMessage</span> <span class="hljs-variable">message</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SimpleMailMessage</span>();
message.setFrom(fromEmail); <span class="hljs-comment">// 发件人</span>
message.setTo(to);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
}
在业务中调用该服务的方法即可
mailService.sendSimpleMail("xxxxxx@xx.com","普通文本邮件","普通文本邮件内容");
发送 html 邮件
为了方便,在原来的 Service 里直接添加一个方法
/**
* 发送 html 邮件
*/
public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
// 注意这里使用的是 MimeMessage
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
// 第二个参数:格式是否为 html
helper.setText(content, true);
mailSender.send(message);
}
调用该方法,直接传入 html 代码的字符串作为正文参数:
mailService.sendHtmlMail("xxxxxx@xx.com","一封 html 测试邮件"," "<div style=\"text-align: center;position: absolute;\" >\n"
+"<h3>\" 一封 html 测试邮件 \"</h3>\n"
+ "<div> 一封 html 测试邮件 </div>\n"
+ "</div>");
像上面直接传递 html 字符串发送 html 邮件,在 java 类里写 html 代码总有点怪怪的,而且有很明显的缺点,若是要用相同样式发送不同的内容,代码冗余度就会增加;而且若是需要发送一个复杂的 html 页面,代码看起来就一团乱麻,而且不方便调整邮件的样式。
我们希望html 和 java 分离开,在 java 里就只管 java,页面代码乖乖到页面文件里面,需要时直接调取该页面文件,整合模板引擎就是一个不错的解决方案。👇
发送基于模板的邮件(以模板引擎 freemarker 为例)
该方法本质上还是发送 html 邮件,只不过是有一个把模板转换成 html 字符串的过程,thymeleaf 也可以实现。这个方法还能使你的系统发出的邮件更加美观。
说明:这里不详细介绍 freemarker 的内容,在这里只描述它的一个使用场景——生成电子邮件,想要进一步了解 freemarker 请行学习
添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
springboot 配置
spring:
freemarker:
cache: false # 缓存配置 开发阶段应该配置为 false 因为经常会改
suffix: .html # 模版后缀名 默认为 ftl
charset: UTF-8 # 文件编码
template-loader-path: classpath:/templates/ # 存放模板的文件夹,以 resource 文件夹为相对路径
在存放模板的文件夹下写一个 html 模板
内容如下:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>freemarker简单示例</title>
</head>
<body>
<h1>Hello Freemarker</h1>
<div>My name is ${myname}</div>
</body>
</html>
仍然为了方便,在原来的 Service 里直接添加代码
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@Test
public void sendTemplateMail(String to, String subject, String template) throws IOException, TemplateException, MessagingException {
// 获得模板
Template template = freeMarkerConfigurer.getConfiguration().getTemplate(template);
// 使用 Map 作为数据模型,定义属性和值
Map<String,Object> model = new HashMap<>();
model.put("myname","ZYF");
// 传入数据模型到模板,替代模板中的占位符,并将模板转化为 html 字符串
String templateHtml = FreeMarkerTemplateUtils.processTemplateIntoString(template,model);
// 该方法本质上还是发送 html 邮件,调用之前发送 html 邮件的方法
this.sendHtmlMail(to, subject, templateHtml);
}
要用的时候调用即可
mailService.sendTemplateMail("xxxxx@xx.com", "基于模板的 html 邮件", "fremarkertemp.html");
发送带附件的邮件
话不多说上代码:
/**
* 发送带附件的邮件
*/
public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
// 要带附件第二个参数设为 true
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(fromEmail);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
<span class="hljs-type">FileSystemResource</span> <span class="hljs-variable">file</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">FileSystemResource</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">File</span>(filePath));
<span class="hljs-type">String</span> <span class="hljs-variable">fileName</span> <span class="hljs-operator">=</span> filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
mailSender.send(message);
}
调用时传入文件路径:
String filePath = "D:\\projects\\springboot\\template.png";
mailService.sendAttachmentsMail("xxxx@xx.com", "带附件的邮件", "邮件中有附件", filePath);
邮箱服务实战
这是我某次实践中邮箱服务的运用,目的是为了能够定时发送邮件,由于发送邮件是耗时操作,为了不妨碍系统处理用户请求,添加了 @Async 注解,定时任务将会在独立的线程中被执行,下面放上链接:
我的踩坑记录
邮件服务器连接失败
org.springframework.mail.MailSendException: Mail server connection failed;
...
nested exception is:
java.net.UnknownHostException: smtp.163.com
...
-
网络问题
控制台输入
ping smtp.163.com
看看是否能 ping 通; -
配置问题
检查一下 application.yml 的邮件服务器配置有没有拼写或格式错误(比如多按了一个空格);
开启 SSL 时使用 587 端口时是无法连接 QQ 邮件服务器的,请使用 465 端口。
授权失败
org.springframework.mail.MailAuthenticationException: Authentication failed;nested exception is
javax.mail.AuthenticationFailedException: 550 User has no permission
...
这个坑是我在教同学时遇到的
按照上文去打开邮箱的 stmp 服务即可解决
消息发送失败
org.springframework.mail.MailSendException: Failed messages:
com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 163 smtp11,D8CowACX7CmSHB5b3SrlCA--.26635S3
点进报错信息提供的网址瞧瞧,是网易官方的退信代码说明,也就是说我们发送的消息被退回来了:
注意到报错信息的退信代码 554,看看 554 是啥来头:
原来是被识别为垃圾邮件了,检查一下邮件的主题及内容,使用合法信息,我当时是因为邮件内容包含test、测试这些字眼所以被拦截了。
总结
spring boot 整合邮件服务并不难,就是踩到坑的时候挺烦的,但这也是学习新知识所必须经历的。
参考链接
Spring Boot 的特性:发送邮件,Arvin Chen