实践应用:Spring Boot项目集成邮件服务

前言

发送邮件应该是网站的必备功能之一,什么注册验证,忘记密码或者是给用户发送营销信息。最早期的时候我们会使用 JavaMail 相关 api 来写发送邮件的相关代码,后来 Spring 推出了 JavaMailSender 更加简化了邮件发送的过程,在之后 Spring Boot 对此进行了封装就有了现在的 spring-boot-starter-mail ,本章文章的介绍主要来自于此包。

简单使用

1、pom 包配置

<dependencies>
	<dependency> 
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-mail</artifactId>
	</dependency> 
</dependencies>

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

spring:
  application:
    name: spirng-boot-mail
  mail:
    default-encoding: UTF-8
    host: smtp.qq.com  #邮箱服务器地址
    password: xxx#不是密码,是授权码
    username: xxxx@qq.com #用户名
    port: 465 端口号
    properties:
     mail:
        smtp:
          starttls:
            enable: true
          #auth: true

3、编写 mailService和其实现类(这里实现了QQ邮箱)

/**
 * 实现qq邮箱发送
 */
@Component
public class QqMailServiceImpl implements MailService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JavaMailSenderImpl sender;

    @Value("${spring.mail.username}")
    private String from;

    private static Properties pro;

    /**
     * 发送文本邮件
     * @param to
     * @param subject
     * @param content
     */

    static {
        pro = System.getProperties(); // 下面各项缺一不可 
        pro.put("mail.smtp.auth", "true"); 
        pro.put("mail.smtp.ssl.enable", "true");
        pro.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    }

    @Override
    public void sendSimpleMail(String to, String subject, String content) {

        sender.setJavaMailProperties(pro);

        MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from); // 发送人
            helper.setTo(to); // 收件人
            helper.setSubject(subject); // 标题
            helper.setText(content); // 内容
            sender.send(message);
            logger.info("简单邮件已经发送。");
        } catch (MessagingException e) {
            logger.error("发送简单邮件时发生异常!", e);
            e.printStackTrace();
        } catch (Exception e) {
            logger.error("发送简单邮件时发生异常!", e);
            e.printStackTrace();
        }
        
    }
 }

4、编写 test 类进行测试

@SpringBootTest
public class MailServiceTest {

    @Qualifier("qqMailServiceImpl")
    @Autowired
    private MailService mailService;

    @Autowired
    private TemplateEngine templateEngine;

    @Test
    public void testSimpleMail() throws Exception {
        mailService.sendSimpleMail("xxxx@qq.com","test simple mail2"," hello this is simple mail....");
    }

至此一个简单的文本发送就完成了。

加点料

但是在正常使用的过程中,我们通常在邮件中加入图片或者附件来丰富邮件的内容,下面讲介绍如何使用 Spring Boot 来发送丰富的邮件。

发送 html 格式邮件

其它都不变在 QqMailServiceImpl添加 sendHtmlMail 方法.

    /**
     * 发送html邮件
     * @param to
     * @param subject
     * @param content
     */
    @Override
    public void sendHtmlMail(String to, String subject, String content) {

        sender.setJavaMailProperties(pro);
        MimeMessage message = sender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            sender.send(message);
            logger.info("html邮件发送成功");
        } catch (MessagingException e) {
            logger.error("发送html邮件时发生异常!", e);
            throw new RuntimeException(e);
        }
    }

在测试类中构建 html 内容,测试发送

 @Test
    public void testHtmlMail() throws Exception {
        String content="<html>\n" +
                "<body>\n" +
                "    <h3 style='color:red'>hello world ! 这是一封html邮件!</h3>\n" +
                "</body>\n" +
                "</html>";
        mailService.sendHtmlMail("xxxx@qq.com","test html mail",content);
    }

发送带附件的邮件

在 QqMailServiceImpl添加 sendAttachmentsMail 方法.

 /**
     * 发送带附件的邮件
     * @param to
     * @param subject
     * @param content
     * @param filePath
     */
    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath){
        sender.setJavaMailProperties(pro);
        MimeMessage message = sender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);
            sender.send(message);
            logger.info("带附件的邮件已经发送。");
        } catch (MessagingException e) {
            logger.error("发送带附件的邮件时发生异常!", e);
            throw new RuntimeException(e);
        }
    }

添加多个附件可以使用多条 helper.addAttachment(fileName, file)

在测试类中添加测试方法

    @Test
    public void sendAttachmentsMail() {
       String filePath="e:\\tmp\\application.log";
       mailService.sendAttachmentsMail("xxxx@qq.com", "主题:带附件的邮件", "有附件,请查收!", filePath);
    }

发送带静态资源的邮件

邮件中的静态资源一般就是指图片,在 QqMailServiceImpl添加 sendAttachmentsMail 方法

 /**
     * 发送正文中有静态资源(图片)的邮件
     * @param to
     * @param subject
     * @param content
     * @param rscPath
     * @param rscId
     */
    @Override
    public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){
        sender.setJavaMailProperties(pro);
        MimeMessage message = sender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            FileSystemResource res = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId, res);

            sender.send(message);
            logger.info("嵌入静态资源的邮件已经发送。");
        } catch (MessagingException e) {
            logger.error("发送嵌入静态资源的邮件时发生异常!", e);
            throw new RuntimeException(e);
        }
    }

在测试类中添加测试方法

  @Test
    public void sendInlineResourceMail() {
        String rscId = "xjdfgkjd";
        String content="<html><body>这是有图片的邮件:<img src=\'cid:" + rscId + "\' ></body></html>";
        String imgPath = "D:\\gitcode\\test\\springboot-demo\\spring-boot-mail\\src\\main\\resources\\detail102.png";

        mailService.sendInlineResourceMail("xxxx@qq.com", "主题:这是有图片的邮件", content, imgPath, rscId);
    }

添加多个图片可以使用多条 <img src='cid:" + rscId + "' >helper.addInline(rscId, res) 来实现

到此所有的邮件发送服务已经完成了。

邮件系统

邮件模板

我们会经常收到这样的邮件:

尊敬的neo用户:
              
          恭喜您注册成为xxx网的用户,,同时感谢您对xxx的关注与支持并欢迎您使用xx的产品与服务。
          ...

其中只有 neo 这个用户名在变化,其它邮件内容均不变,如果每次发送邮件都需要手动拼接的话会不够优雅,并且每次模板的修改都需要改动代码的话也很不方便,因此对于这类邮件需求,都建议做成邮件模板来处理。模板的本质很简单,就是在模板中替换变化的参数,转换为 html 字符串即可,这里以thymeleaf为例来演示。

1、pom 中导入 thymeleaf 的包

  <!-- 模板引擎 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2、在 resorces/templates 下创建 emailTemplate.html

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8"/>
        <title>Title</title>
    </head>
    <body>
        您好,这是验证邮件,请点击下面的链接完成验证,<br/>
        <a href="#" th:href="@{ http://www.newtouch.com/active/{id}(id=${id}) }">激活账号</a>
    </body>
</html>

3、解析模板并发送

   @Test
    public void sendTemplateMail() {
        //创建邮件正文
        Context context = new Context();
        context.setVariable("id", "006");
        String emailContent = templateEngine.process("emailTemplate", context);

        mailService.sendHtmlMail("ityouknow@126.com","主题:这是模板邮件",emailContent);
    }

发送失败

因为各种原因,总会有邮件发送失败的情况,比如:邮件发送过于频繁、网络异常等。在出现这种情况的时候,我们一般会考虑重新重试发送邮件,会分为以下几个步骤来实现:

  1. 接收到发送邮件请求,首先记录请求并且入库。
  2. 调用邮件发送接口发送邮件,并且将发送结果记录入库。
  3. 启动定时系统扫描时间段内,未发送成功并且重试次数小于3次的邮件,进行再次发送

异步发送

很多时候邮件发送并不是我们主业务必须关注的结果,比如通知类、提醒类的业务可以允许延时或者失败。这个时候可以采用异步的方式来发送邮件,加快主交易执行速度,在实际项目中可以采用MQ发送邮件相关参数,监听到消息队列之后启动发送邮件。