[关闭]
@javazjm 2017-11-01T12:53:05.000000Z 字数 4108 阅读 1322

Springboot系列学习十四:邮件发送

Springboot mail


1. 引入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. </dependency>

2.在application.properties中添加邮箱配置

使用用户名密码的方式

  1. spring.mail.default-encoding=UTF-8
  2. # 邮箱服务器地址
  3. spring.mail.host=10.6.4.78
  4. # 用户名
  5. spring.mail.username=zhangjinmiao@zihexin.com
  6. # 密码
  7. spring.mail.password=xxxxxx
  8. # 邮件发送人
  9. mail.fromMail.addr=zhangjinmiao@zihexin.com

使用用户名授权码的方式(比如QQ)

  1. spring.mail.default-encoding=UTF-8
  2. # 邮箱服务器地址
  3. spring.mail.host=smtp.qq.com
  4. # 用户名
  5. spring.mail.username=1539745948@qq.com
  6. # 密码
  7. spring.mail.password=xxxxxxxxxxx
  8. # 客户端授权验证
  9. spring.mail.properties.mail.smtp.auth=true
  10. # 始终使用安全设置
  11. spring.mail.properties.mail.smtp.starttls.enable=true
  12. # 安全设置必须
  13. spring.mail.properties.mail.smtp.starttls.required=true
  14. # 邮件发送人
  15. mail.fromMail.addr=1539745948@qq.com

3.邮件服务接口

  1. public interface MailService {
  2. /**
  3. * Send a text mail
  4. * @param to 接收人
  5. * @param subject 主题
  6. * @param content 内容
  7. */
  8. public void sendSimpleMail(String to, String subject, String content);
  9. /**
  10. * Send HTML mail
  11. * @param to 接收人
  12. * @param subject 主题
  13. * @param content 内容
  14. */
  15. public void sendHtmlMail(String to, String subject, String content);
  16. /**
  17. * Send an email with attachments
  18. * @param to 接收人
  19. * @param subject 主题
  20. * @param content 内容
  21. * @param filePath 附件地址
  22. */
  23. public void sendAttachmentsMail(String to, String subject, String content, String filePath);
  24. /**
  25. * 发送正文中有静态资源(图片)的邮件
  26. * @param to 接收人
  27. * @param subject 主题
  28. * @param content 内容
  29. * @param rscPath 静态文件名
  30. * @param rscId 静态文件地址
  31. */
  32. public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId);
  33. }

4. 服务实现类

具体查看代码,springboot 已经封装了host,port,username,password属性的设置,所以实现类中没有出现,详见类。

  1. @Component
  2. public class MailServiceImpl implements MailService {
  3. // ...
  4. }

5. 测试

特别注意,发送模板文件
1. 在resources下新建templates\emailTemplate.html

  1. 代码中

    //创建邮件正文
    Context context = new Context();
    context.setVariable("id", "006");
    String emailContent = templateEngine.process("emailTemplate", context);
    
    mailService.sendHtmlMail(to, "主题:这是模板邮件", emailContent);
    

改造1

1. 封装邮件发送参数为实体

  1. public class Email implements Serializable {
  2. private static final long serialVersionUID = 1L;
  3. //必填参数
  4. private String[] email;//接收方邮件
  5. private String subject;//主题
  6. private String content;//邮件内容
  7. //选填
  8. private String template;//模板
  9. private HashMap<String, String> kvMap;// 自定义参数
  10. public Email() {
  11. super();
  12. }
  13. public Email(String[] email, String subject, String content, String template, HashMap<String, String> kvMap) {
  14. this.email = email;
  15. this.subject = subject;
  16. this.content = content;
  17. this.template = template;
  18. this.kvMap = kvMap;
  19. }
  20. // getter setter ...
  21. }

2. 邮件接口

见IMailService

3. 接口实现类

见MailServiceImpl2,包括Thymeleaf和Freemarker模板,发送1000次,建议使用Freemarker模板。

改造2

邮件队列1

解决短时间内频繁发送邮件引起邮件服务器报警,邮件发送失败的情况,使用队列来对邮件发送进行流量削峰、间隔发送以及重复内容检测。

1. 创建邮件队列

MailQueue

2. 创建消费队列

ConsumeMailQueue

问题

LinkedBlockingQueue是进程内的队列,容器挂掉后,队列中的内容就丢了。所以我们推荐使用Redis队列,
只要redis没挂掉,即使邮件服务挂掉也不担心。

redis邮件队列

1. 引入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-redis</artifactId>
  4. <version>1.4.3.RELEASE</version>
  5. </dependency>

2. 服务

  1. public void sendRedisQueue(Email mail) throws Exception {
  2. redisTemplate.convertAndSend("mail", mail);
  3. }

3. redis配置

监听器监听队列mail是否有邮件进入,有的话调用方法receiveMessage消费队列开始发送邮件。

可加入定时扫描,发送失败的邮件

  1. 定时方法
  1. /**
  2. * 统计失败邮件定时重新发送
  3. */
  4. @Component("sendMail")
  5. public class SendMail {
  6. //@Scheduled(cron = "0/5 * * * * ?")
  7. public void sendMail() {
  8. System.out.println("同步开始");
  9. }
  10. }
  1. 配置文件 spring-context-task.xml

或者使用注解@Scheduled和@EnableScheduling,
这样就不需要配置文件spring-context-task.xml了。

将邮件服务以dubbo的形式发布

  1. 引入依赖
  1. <!-- DubboX相关 -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>dubbo</artifactId>
  5. <!-- 这里使用最新的2.8.4版本,中央仓库不存在,请自行打入本地仓库 -->
  6. <!-- 百度网盘:http://pan.baidu.com/s/1gfxiuYZ -->
  7. <version>2.8.4</version>
  8. <exclusions>
  9. <exclusion>
  10. <artifactId>spring</artifactId>
  11. <groupId>org.springframework</groupId>
  12. </exclusion>
  13. </exclusions>
  14. </dependency>
  15. <!-- zookeeper 第三方操作工具类 -->
  16. <dependency>
  17. <groupId>com.101tec</groupId>
  18. <artifactId>zkclient</artifactId>
  19. <version>0.6</version>
  20. <exclusions>
  21. <exclusion>
  22. <groupId>org.slf4j</groupId>
  23. <artifactId>slf4j-log4j12</artifactId>
  24. </exclusion>
  25. </exclusions>
  26. </dependency>
  1. 在实现类上使用注解
    com.alibaba.dubbo.config.annotation.Service

  2. 配置文件
    spring-context-dubbo.xml

参考:

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注