Spring Boot 使用Thymeleaf发送模板邮件

2019-07-10 wjy329 SpringBoot 24

通常情况下,我们收到的邮件都是统一的模板,例如:
尊敬的xxx:      您的专属会员激活码为:xxxxxx。   
这里面的xxx和xxxxx都代表动态的参数,而且整个也是写好的模板,下面我们就来实现这样一个模板:
简单邮件的发送可以参考:Springboot简单邮件发送


1. pom中添加Thymeleaf包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>


2.在resources/templates 下创建模板:mailTemplates.html
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
您好,您之前在<a href="https://www.wjy329.com" style="font-weight: bold">WJY329的博客</a>中的评论有新的回复了!!!<br/>
<span style="font-weight: bold">评论文章地址:</span><a href="#" th:href="@{'https://www.wjy329.com/blog/article/'+${articleId}+'.html'}">原文地址</a><br>
<span style="font-weight: bold">您的评论:</span><span th:utext="${comment}"></span><br>
<span style="font-weight: bold">回复内容:</span><span th:utext="${content}"></span><br>
</body>
</html>

3. 注入模板引擎
     @Autowired     
    TemplateEngine templateEngine;

4. 将内容写入模板
//创建邮件正文
Context context = new Context();
context.setVariable("articleId", comment.getArticleId());
context.setVariable("comment",comment.getContent());
context.setVariable("content",reply.getContent());
String emailContent = templateEngine.process("mailTemplates", context);


5.发模板邮件
MimeMessage message = mailSender.createMimeMessage();
try {
//重点
MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
helper.setFrom(from);
helper.setTo(userEmail);
helper.setSubject(subject);
helper.setText(emailContent, true);
mailSender.send(message);
System.out.println("邮件发送成功!");
} catch (Exception e) {
System.out.println("邮件发送失败!"+"---"+e.toString());
}
这样就可以了。邮件示例:



6.完整代码:
package com.wjy329.wblog.service.impl;
/**
* @Author wjy329
* @Time 2019/2/2312:41 PM
* @description
*/

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.internet.MimeMessage;

@Service
public class MailServiceImpl implements MailService{
@Autowired
private JavaMailSender mailSender;
@Autowired
TemplateEngine templateEngine;

@Value("${mail.fromMail.addr}")
private String from;


@Override
public void sendCommentTempMail(String userStr, String subject, Comment comment, Comment reply) {
//创建邮件正文
Context context = new Context();
context.setVariable("articleId", comment.getArticleId());
context.setVariable("comment",comment.getContent());
context.setVariable("content",reply.getContent());
String emailContent = templateEngine.process("mailTemplates", context);
sendTempEmail(userStr, subject, emailContent);
}
private void sendTempEmail(String userStr, String subject, String emailContent) {
MimeMessage message = mailSender.createMimeMessage();
try {
//重点
MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
helper.setFrom(from);
helper.setTo(userStr);
helper.setSubject(subject);
helper.setText(emailContent, true);
mailSender.send(message);
System.out.println("邮件发送成功!");
} catch (Exception e) {
System.out.println("邮件发送失败!"+"---"+e.toString());
}
}
}
模板中的内容可以随意定制,关于Thymeleaf的更多资料,请自行百度。

参考:纯洁的微笑   http://www.ityouknow.com/springboot/2017/05/06/spring-boot-mail.html
本文作者:wjy329
版权声明:本博客除特殊说明外均属本人原创。如需转载请署名作者及文章出处。
评论