由于业务需要,公司SpringBoot项目集成邮件发送功能。
1:引入pom依赖
<!--邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2:yml进行配置
spring:
mail:
# 设置邮箱主机
host: *.com #smtp.126.com
# 设置用户名
username: tzmarlon@163.com
# 设置密码,该处的密码是QQ邮箱开启SMTP的授权码而非QQ密码
password: ENC(8O5MxGRDMvW8MsFmQPV38OCi/ha) #nse123
port: 587
default-encoding: UTF-8
properties:
mail:
smtp:
auth: true
thymeleaf:
suffix: .html
cache: false
prefix: classpath:/templates # 存放模板的文件夹,以resource文件夹为相对路径
mode: LEGACYHTML5
encoding: utf-8
servlet:
content-type: text/html
mailconfig:
# 邮件主题
title: 以下需求和问题单已经超过X天没分发,请及时分发
# 收件人邮箱
toAddrs: email
# 抄送人邮箱
ccAddrs:
#收件人邮箱
from:
3:编写邮件工具类将配置文件的mailconfig对象注入到java对象中
@Configuration
@ConfigurationProperties(prefix = "mailconfig")
public class MailDO {
//发件人邮箱
private String from;
//标题
private String title;
//内容
private String content;
//接收人邮件地址
private String toAddrs;
//抄送人地址
private String ccAddrs;
//附加,value 文件的绝对地址/动态模板数据
private Map<String, Object> attachment;
}
4:编写发送邮件业务类
/**
* 发送模板邮件 使用thymeleaf模板
* 若果使用freemarker模板
* Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
* configuration.setClassForTemplateLoading(this.getClass(), "/templates");
* String emailContent = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("mail.ftl"), params);
* @param mailDO
*/
public void sendTemplateMail(MailDO mailDO) {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
messageHelper.setFrom(mailDO.getFrom());// 发送人的邮箱
//多个收件人
if (StringUtils.isNotEmpty(mailDO.getToAddrs())){
messageHelper.setTo(mailDO.getToAddrs().split(","));//发给谁 对方邮箱
messageHelper.setSubject(mailDO.getTitle()); //标题
//注意这里需要用cc才能看到抄送人,如果用bcc是隐藏抄送人的
messageHelper.setCc(mailDO.getCcAddrs().split(","));
//使用模板thymeleaf
//Context是导这个包import org.thymeleaf.context.Context;
Context context = new Context();
//定义模板数据
context.setVariables(mailDO.getAttachment());
//获取thymeleaf的html模板
String emailContent = templateEngine.process("/mail/mail",context); //指定模板路径
messageHelper.setText(emailContent,true);
//发送邮件
javaMailSender.send(mimeMessage);
}
} catch (MessagingException e) {
log.error("模板邮件发送失败->message:{}",e.getMessage());
}
}
5:模板页面
在resources下新建templates/mail/mail.html文件,文件内容如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
table{border-collapse:collapse;margin:0 auto;text-align:center}
table td,table th{border:1px solid #cad9ea;color:#666;height:30px}
table thead th{background-color:#CCE8EB;width:100px}
table tr:nth-child(odd){background:#fff}
table tr:nth-child(even){background:#F5FAFA}
a{text-decoration: none}
</style>
</head>
<body>
<table border="1px solid #333">
<thead><th>IssueId</th><th>创建人</th><th>任务名称</th><th>创建时间</th><th>状态</th><th>任务类型</th></thead>
<tbody>
<tr th:each="issue,userStat:${issueTaskInfos}">
<td><a th:href="@{'url' + ${issue.issueId}}" target="_blank" th:text="${issue.issueId}"></a></td>
<td th:text="${issue.author}"></td>
<td align="left" th:text="${issue.issueName}"></td>
<td th:text="${#dates.format(issue.createdTime,'yyyy-MM-dd HH:mm:ss')}"></td>
<td th:text="${issue.state}"></td>
<td th:text="${issue.issueType}"></td>
</tr>
</tbody>
</table>
</body>
</html>