基础依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- 支持发送邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
定义业务接口
public interface EmailService {
void sendTemplateEmail(EmailData data, String templateName);
}
接口实现
@Slf4j
@Service
public class EmailServiceImpl implements EmailService {
//邮件的发送者
@Value("${spring.mail.username}")
private String from;
//注入MailSender
@Autowired
private JavaMailSender mailSender;
//发送邮件的模板引擎
@Autowired
private FreeMarkerConfigurer configurer;
/**
* @param data 发送邮件数据对象
* @param templateName 模板名称
*/
@Override
public void sendTemplateEmail(EmailData data, String templateName) {
try {
// 通过JavaMailSender对象创建一个MimeMessage对象
MimeMessage mimeMessage = mailSender.createMimeMessage();
// 用MimeMessageHelper对象对MimeMessage对象进行各种设置
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, StandardCharsets.UTF_8.toString());
helper.setFrom(from); // 发送方邮箱地址
helper.setTo(data.getManagerEmail()); // 接收方邮箱地址
helper.setSubject(data.getSubject()); // 设置标题
// FreeMarker通过Map进行值映射,因此创建一个Map对象用于传值
HashMap<String, Object> map = new HashMap<>();
// 存入数据模型对象
map.put("data", data);
// 根据模板名称获取 FreeMarker 模板(需要加尾缀如 test.ftl)
Template template = configurer.getConfiguration().getTemplate(templateName);
// 把模板转换成text格式的文本,期间把Map中的数据替换到模板中
String context = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
// 把转换后的模板文本设置到MimeMessage中,并设置其为html格式
helper.setText(context, true);
// 发送邮件
mailSender.send(mimeMessage);
log.info("发送邮件完成");
} catch (MessagingException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (MalformedTemplateNameException e) {
e.printStackTrace();
} catch (TemplateNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}
}
配置文件
# 邮件编码格式
spring.mail.default-encoding=UTF-8
# 协议(smtp)
spring.mail.protocol=smtp
spring.mail.host=smtp.163.com
# 端口
spring.mail.port=25
# 发送方的邮箱账号
spring.mail.username=echo2787@163.com
# 发送方的邮箱客户端授权码(需要在邮箱开启stmp协议,并用授权码登录,而非密码)
spring.mail.password=RMTHNEUBNJQERPXY
模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>${data.subject}</title>
<link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.1/jquery.form.min.js"
integrity="sha384-tIwI8+qJdZBtYYCKwRkjxBGQVZS3gGozr3CtI+5JF/oL1JmPEHzCEnIKbDbLTCer"
crossorigin="anonymous"></script>
<script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<table class="table" border="0" style="width: 40%">
<tr>
<td style="font-size: 30px;color: red" align="right">主机改动通知</td>
</tr>
<tr>
<td align="right">操作结果:</td>
<td align="left">${data.success}</td>
</tr>
<tr>
<td align="right">最近操作日期:</td>
<td align="left">${data.latestDate?string('yyyy-MM-dd HH:mm:ss')}</td>
</tr>
<tr>
<td align="right">原主机名:</td>
<td align="left">${data.oldHostName}</td>
</tr>
<tr>
<td align="right">新主机名:</td>
<td align="left">${data.hostName}</td>
</tr>
<tr>
<td align="right">管理员邮箱:</td>
<td align="left">${data.managerEmail}</td>
</tr>
<tr>
<td align="right">操作者:</td>
<td align="left">${data.operatorName}</td>
</tr>
<tr>
<td align="right">操作者手机:</td>
<td align="left">${data.phone}</td>
</tr>
<tr>
<td align="right">备注:</td>
<td align="left">${data.description}</td>
</tr>
<tr>
<td align="right">状态:</td>
<td align="left">${data.status}</td>
</tr>
<tr>
<td align="right"></td>
<td align="right"><a href="http://www.wipinfo.com">Xxx信息科技有限公司</a></td>
</tr>
</table>
</div>
</body>
</html>
效果