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.boot</groupId >
<artifactId > spring-boot-starter-mail</artifactId >
</dependency >
<dependency >
<groupId > org.springframework.boot</groupId >
<artifactId > spring-boot-starter-thymeleaf</artifactId >
</dependency >
<dependency >
<groupId > org.springframework.boot</groupId >
<artifactId > spring-boot-starter-freemarker</artifactId >
</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<String, String> 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;
<span class="hljs-meta">@Override</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">send</span><span class="hljs-params">(Email mail)</span> <span class="hljs-keyword">throws</span> Exception </span>{
MailUtil mailUtil = <span class="hljs-keyword">new</span> MailUtil();
SimpleMailMessage message = <span class="hljs-keyword">new</span> SimpleMailMessage();
message.setFrom(USER_NAME);
message.setTo(mail.getEmail());
message.setSubject(mail.getSubject());
message.setText(mail.getContent());
mailUtil.start(mailSender, message);
}
<span class="hljs-meta">@Override</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">sendHtml</span><span class="hljs-params">(Email mail)</span> <span class="hljs-keyword">throws</span> Exception </span>{
MailUtil mailUtil = <span class="hljs-keyword">new</span> MailUtil();
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = <span class="hljs-keyword">new</span> MimeMessageHelper(message, <span class="hljs-keyword">true</span>);
helper.setFrom(USER_NAME);
helper.setTo(mail.getEmail());
helper.setSubject(mail.getSubject());
helper.setText(
<span class="hljs-string">"<html><body><img src=\"cid:springcloud\" ></body></html>"</span>,
<span class="hljs-keyword">true</span>);
<span class="hljs-comment">// 发送图片</span>
File file = ResourceUtils.getFile(<span class="hljs-string">"classpath:static"</span>
+ Constants.SF_FILE_SEPARATOR + <span class="hljs-string">"image"</span>
+ Constants.SF_FILE_SEPARATOR + <span class="hljs-string">"springcloud.png"</span>);
helper.addInline(<span class="hljs-string">"springcloud"</span>, file);
<span class="hljs-comment">// 发送附件</span>
file = ResourceUtils.getFile(<span class="hljs-string">"classpath:static"</span>
+ Constants.SF_FILE_SEPARATOR + <span class="hljs-string">"file"</span>
+ Constants.SF_FILE_SEPARATOR + <span class="hljs-string">"关注科帮网获取更多源码.zip"</span>);
helper.addAttachment(<span class="hljs-string">"科帮网"</span>, file);
mailUtil.startHtml(mailSender, message);
}
<span class="hljs-meta">@Override</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">sendFreemarker</span><span class="hljs-params">(Email mail)</span> <span class="hljs-keyword">throws</span> Exception </span>{
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = <span class="hljs-keyword">new</span> MimeMessageHelper(message, <span class="hljs-keyword">true</span>);
helper.setFrom(USER_NAME);
helper.setTo(mail.getEmail());
helper.setSubject(mail.getSubject());
Map<String, Object> model = <span class="hljs-keyword">new</span> HashMap<String, Object>();
model.put(<span class="hljs-string">"content"</span>, mail.getContent());
Template template = configuration.getTemplate(mail.getTemplate()+<span class="hljs-string">".flt"</span>);
String text = FreeMarkerTemplateUtils.processTemplateIntoString(
template, model);
helper.setText(text, <span class="hljs-keyword">true</span>);
mailSender.send(message);
}
<span class="hljs-meta">@Override</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">sendThymeleaf</span><span class="hljs-params">(Email mail)</span> <span class="hljs-keyword">throws</span> Exception </span>{
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = <span class="hljs-keyword">new</span> MimeMessageHelper(message, <span class="hljs-keyword">true</span>);
helper.setFrom(USER_NAME);
helper.setTo(mail.getEmail());
helper.setSubject(mail.getSubject());
Context context = <span class="hljs-keyword">new</span> Context();
context.setVariable(<span class="hljs-string">"email"</span>, mail);
String text = templateEngine.process(mail.getTemplate(), context);
helper.setText(text, <span class="hljs-keyword">true</span>);
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);
}
<span class="hljs-meta">@Override</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">(String... args)</span> <span class="hljs-keyword">throws</span> Exception </span>{
Email mail = <span class="hljs-keyword">new</span> Email();
mail.setEmail(<span class="hljs-string">"345849402@qq.com"</span>);
mail.setSubject(<span class="hljs-string">"你个小逗比"</span>);
mail.setContent(<span class="hljs-string">"科帮网欢迎您"</span>);
mail.setTemplate(<span class="hljs-string">"welcome"</span>);
mailService.sendFreemarker(mail);
}
}
好了,不出意外,这四种方式应该绝对是没问题的,小伙伴们就等着右下角弹窗窗吧。
最后,做了一个小小的评测,生成模版时间对比 (1000 次循环)
Thymeleaf 用时:2686ms Freemarker 用时:498ms
对比测试,建议使用 Freemarker 模版。
项目:http://git.oschina.net/52itstyle/spring-boot-mail
转自:https://blog.52itstyle.vip/archives/1264/