前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot开发案例之整合mail发送服务

SpringBoot开发案例之整合mail发送服务

作者头像
小柒2012
发布2018-04-16 13:55:38
8140
发布2018-04-16 13:55:38
举报
文章被收录于专栏:IT笔记

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中引入以下依赖:

代码语言:javascript
复制
       <!-- email -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <!-- thymeleaf 模版 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- freemarker 模版 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

application.properties中配置以下内容:

代码语言:javascript
复制
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:

代码语言:javascript
复制
/**
 * 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高(评测对比见文章底部)。

代码语言:javascript
复制
@Service
public class MailServiceImpl implements IMailService {
    @Autowired
    private JavaMailSender mailSender;//执行者
    @Autowired
    public Configuration configuration;//freemarker
    @Autowired
    private SpringTemplateEngine  templateEngine;//thymeleaf
    @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:

代码语言:javascript
复制
@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("345849402@qq.com");
        mail.setSubject("你个小逗比");
        mail.setContent("科帮网欢迎您");
        mail.setTemplate("welcome");
        mailService.sendFreemarker(mail);
    }
}

好了,不出意外,这四种方式应该绝对是没问题的,小伙伴们就等着右下角弹窗窗吧。

最后,做了一个小小的评测,生成模版时间对比(1000次循环)

  • Thymeleaf用时:2686ms
  • Freemarker用时:498ms

对比测试,建议使用Freemarker模版。

项目:http://git.oschina.net/52itstyle/spring-boot-mail

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-07-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 开发环境
  • 项目结构
  • 引入依赖
  • 封装实体
  • 业务实现
  • 测试用例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档