spring-boot-mail.jpg
记得上个月做过这样一篇笔记,微服务架构实践之邮件通知系统改造。
当时用的是开源的第三方插件 mail 和 Thymeleaf 做发送服务,显然这样子你需要配置一些东西,麻烦!!!接触了 Spring Boot 以后,显然有更好的解决方案,我们只需要引入 spring-boot-starter-mail 模块就实现了自动化配置。
好,废话不扯,言归正题 (文中大部分是部分代码,详细代码见 Git)。
开发环境
JDK1.7、Maven、Eclipse、SpringBoot1.5.2、spring-boot-starter-mail、spring-boot-starter-thymeleaf,spring-boot-starter-freemarker
项目结构
springboot-mail.png
引入依赖
在工程中的 pom.xml 中引入以下依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-freemarkerartifactId>
dependency>
application.properties 中配置以下内容:
spring.mail.host=smtp.qq.com
spring.mail.username=345849402@qq.com
#授权码g,在QQ邮箱客户端生成 修改成自己的 设置-账户-开启服务-获取授权码
spring.mail.password=XXXXXXX
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
#freemarker
spring.freemarker.template-loader-path=classpath:/static/template/
spring.freemarker.enabled=true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.allow-request-override=false
spring.freemarker.check-template-location=true
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#thymeleaf
spring.thymeleaf.prefix=classpath:/static/template/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
封装实体
首先我们,封装一个 Email 实体 Email.java:
- Email封装类
- 创建者 科帮网
- 创建时间 2017年7月20日
*/
public class Email implements Serializable {
private static final long serialVersionUID = 1L;
private String email;
private String subject;
private String content;
private String template;
private HashMap kvMap;
… 省略 get set
业务实现
既然用了 spring,就按照 spring 的方式来,先定义一个接口 IMailService,接着是实现 MailServiceImpl。
以下代码,实现了四种方式:纯文本,富文本 (图片,附件),Freemarker 模版以及 Thymeleaf 模版。
这里需要注意的是,springboot 1.4.0 以后 Velocity 废弃了,官方建议用 freemaker。而 thymeleaf 是博主自己实现的,显然效率没有 freemaker 高 (评测对比见文章底部)。
@Service
public class MailServiceImpl implements IMailService {
@Autowired
private JavaMailSender mailSender;
@Autowired
public Configuration configuration;
@Autowired
private SpringTemplateEngine templateEngine;
@Value("${spring.mail.username}")
public String USER_NAME;
@Override
public void send(Email mail) throws Exception {
MailUtil mailUtil = new MailUtil();
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(USER_NAME);
message.setTo(mail.getEmail());
message.setSubject(mail.getSubject());
message.setText(mail.getContent());
mailUtil.start(mailSender, message);
}
@Override
public void sendHtml(Email mail) throws Exception {
MailUtil mailUtil = new MailUtil();
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(USER_NAME);
helper.setTo(mail.getEmail());
helper.setSubject(mail.getSubject());
helper.setText(
"<html><body><img src=\"cid:springcloud\" ></body></html>",
true);
File file = ResourceUtils.getFile("classpath:static"
+ Constants.SF_FILE_SEPARATOR + "image"
+ Constants.SF_FILE_SEPARATOR + "springcloud.png");
helper.addInline("springcloud", file);
file = ResourceUtils.getFile("classpath:static"
+ Constants.SF_FILE_SEPARATOR + "file"
+ Constants.SF_FILE_SEPARATOR + "关注科帮网获取更多源码.zip");
helper.addAttachment("科帮网", file);
mailUtil.startHtml(mailSender, message);
}
@Override
public void sendFreemarker(Email mail) throws Exception {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(USER_NAME);
helper.setTo(mail.getEmail());
helper.setSubject(mail.getSubject());
Map<String, Object> model = new HashMap<String, Object>();
model.put("content", mail.getContent());
Template template = configuration.getTemplate(mail.getTemplate()+".flt");
String text = FreeMarkerTemplateUtils.processTemplateIntoString(
template, model);
helper.setText(text, true);
mailSender.send(message);
}
@Override
public void sendThymeleaf(Email mail) throws Exception {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(USER_NAME);
helper.setTo(mail.getEmail());
helper.setSubject(mail.getSubject());
Context context = new Context();
context.setVariable("email", mail);
String text = templateEngine.process(mail.getTemplate(), context);
helper.setText(text, true);
mailSender.send(message);
}
}
测试用例
老司机带你去开车 SpringbootMailApplication.java:
@SpringBootApplication
@ComponentScan(basePackages={“com.itstyle.mail”})
public class SpringbootMailApplication implements CommandLineRunner {
@Autowired
private IMailService mailService;
public static void main(String[] args) {
SpringApplication.run(SpringbootMailApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Email mail = new Email();
mail.setEmail("[email protected]");
mail.setSubject("你个小逗比");
mail.setContent("科帮网欢迎您");
mail.setTemplate("welcome");
mailService.sendFreemarker(mail);
}
}
好了,不出意外,这四种方式应该绝对是没问题的,小伙伴们就等着右下角弹窗窗吧。
最后,做了一个小小的评测,生成模版时间对比 (1000 次循环)
- Thymeleaf 用时:2686ms
- Freemarker 用时:498ms
对比测试,建议使用 Freemarker 模版。
项目:http://git.oschina.net/52itstyle/spring-boot-mail
转自:https://blog.52itstyle.vip/archives/1264/