邮件服务已经是基础性服务了 ,是网站的必备功能之一,当注册了某些网站的时候,邮箱里通常会收到一封注册成功通知邮件或者点击激活账号的邮件,博客园也是如此。本文使用Spring Boot,通过QQ邮箱来模仿博客园发送一封通知邮件。
博客园发送的“欢迎您加入博客园”的主题邮件如图所示。这种通知邮件,只有登录用户名在变化,其它邮件内容均不变,很适合用邮件模板来处理。
模板可以实现显示与数据分离,将模板文件和数据通过模板引擎生成最终的HTML代码。
Thymeleaf是一个适用于Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本。Thymeleaf由于使用了标签属性做为语法,模版页面直接用浏览器渲染,与其它模板引擎(比如Freemaker)相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用。
Thymeleaf作为Spring官方推荐的模板引擎,Spring boot对Thymeleaf支持比较友好,配置简单,这里使用Thymeleaf作为模板引擎。
下面正式开始实现仿博客园发送通知邮件。
1. pom.xml添加邮件和模板相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency></pre>
2. application.property配置邮箱和thymelea模板
我使用的是QQ邮箱,需要获得QQ邮箱的授权码。
spring.thymeleaf.prefix
=classpath:/templates/<span style="color: #000000;">spring.thymeleaf.suffix=<span style="color: #000000;">.html
spring.thymeleaf.mode=<span style="color: #000000;">HTML
spring.thymeleaf.encoding=UTF-8<span style="color: #000000;">
spring.thymeleaf.servlet.content-type:text/<span style="color: #000000;">html
热部署文件,页面不产生缓存,及时更新
spring.thymeleaf.cache
=<span style="color: #0000ff;">false<span style="color: #000000;">spring.resources.chain.strategy.content.enabled=<span style="color: #0000ff;">true<span style="color: #000000;">
spring.resources.chain.strategy.content.paths=<span style="color: #008000;">/**
3. 编写Service及其实现
Service中有两个方法:
sendSimpleMail用于发送简单的文本邮件,是一个比较基础的案例。
sendHtmlMail用于发送HTML邮件,发送通知邮件用的就是这个方法。其实模板邮件也就是HTML邮件中的一个子类。
MailService:
MailServiceImpl:
</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">final</span> Logger logger = LoggerFactory.getLogger(<span style="color: #0000ff;">this</span><span style="color: #000000;">.getClass());
@Autowired
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> JavaMailSender mailSender;
@Value(</span>"${spring.mail.username}"<span style="color: #000000;">)
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String from;
@Override
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> sendSimpleMail(String to,String content) {
SimpleMailMessage message </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> SimpleMailMessage();
message.setFrom(from);
message.setTo(to);</span><span style="color: #008000;">//</span><span style="color: #008000;">邮件接收者</span>
message.setSubject(subject);<span style="color: #008000;">//</span><span style="color: #008000;">邮件主题</span>
message.setText(content);<span style="color: #008000;">//</span><span style="color: #008000;">邮件内容</span>
<span style="color: #0000ff;">try</span><span style="color: #000000;"> {
mailSender.send(message);
logger.info(</span>"发送简单邮件成功!"<span style="color: #000000;">);
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
logger.error(</span>"发送简单邮件时发生异常!"<span style="color: #000000;">,e);
}
}
@Override
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> sendHtmlMail(String to,String content) {
MimeMessage message </span>=<span style="color: #000000;"> mailSender.createMimeMessage();
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
</span><span style="color: #008000;">//</span><span style="color: #008000;">true表示需要创建一个multipart message</span>
MimeMessageHelper helper = <span style="color: #0000ff;">new</span> MimeMessageHelper(message,<span style="color: #0000ff;">true</span><span style="color: #000000;">);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,</span><span style="color: #0000ff;">true</span><span style="color: #000000;">);
mailSender.send(message);
logger.info(</span>"发送HTML邮件成功!"<span style="color: #000000;">);
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (MessagingException e) {
logger.error(</span>"发送HTML邮件时发生异常!"<span style="color: #000000;">,e);
}
}
}
4. 创建模板
在resorces/templates下创建emailTemplate.html模板,与模板配置中的spring.thymeleaf.prefix=classpath:/templates/对应,不然会找不到模板。
关于Thymeleaf的使用这里简单介绍一下:
引入命名空间:
访问数据:#{user.name}
访问变量:${today}
emailTemplate.html:
5. JUnit单元测试
使用Junit进行单元测试,pom.xml中已经默认配置好了,需要编写测试类和测试方法。测试类以xxxTest.java命名,测试方法上面加
@Test注解就可以使用了。具体代码如下:
@Autowired
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> MailService mailService;
@Autowired
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> TemplateEngine templateEngine;
@Test
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> testSendSimpleMail() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {
mailService.sendSimpleMail(</span>"[email protected]","测试发送简单文本邮件","测试发送简单文本邮件"<span style="color: #000000;">);
}
@Test
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> testSendTemplateMail() {
Context context </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Context();
context.setVariable(</span>"username","shangguanhao"<span style="color: #000000;">);
String emailContent </span>= templateEngine.process("emailTemplate"<span style="color: #000000;">,context);
mailService.sendHtmlMail(</span>"[email protected]","欢迎您加入博客园"<span style="color: #000000;">,emailContent);
}
}
进行Junit测试,就可以发送一个简答的文本邮件和一个HTML的模板邮件,几乎和博客园的一模一样(如下图所示):