springboot整合邮箱功能二(普通邮件, html邮件, thymleaf邮件)

1. 准备工作


1.1 qq邮箱设置


本文默认使用qq邮箱来发送邮件,然后使用一个在线临时邮箱来接收邮件。 为了让程序能够通过qq邮箱来发送邮件,必须在qq邮箱中配置一下打开IMAP/SMTP服务



开启的时候,需要通过手机发送一条信息作为验证,验证成功后,会给你一个授权码,这个后面我们要用到




2. SpringBoot项目配置


2.1 springboot项目中引入email依赖




    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        
        2.5.5
         
    
    
        UTF-8
        UTF-8
        1.8
        spring-boot-email

        8
        8
    

    org.example
    ${project-name}
    1.0-SNAPSHOT
    ${project-name}
    ${project-name}

    
        
        
            org.springframework.boot
            spring-boot-starter-mail
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
            com.alibaba.fastjson2
            fastjson2
            2.0.25
        

        
            org.apache.commons
            commons-lang3
        

        
            com.google.guava
            guava
            31.1-android
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.7.1
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


2.2 application.yml配置信息


server:
  port: 8000
  max-http-header-size: 8192
  servlet:
    encoding:
      charset: UTF-8
      force: true
      enabled: true

#配置日志
logging:
  level:
    root: info
spring:
  application:
    name: spring-boot-email
  mvc.async.request-timeout: 20000

  mail:
    #    host: "smtp.163.com" # 发件服务器地址
    #    port: 25 # 常用邮件端口25、109、110、143、465、995、993、994 如果开启了SSL安全则使用对应的端口号,25为非加密端口号
    #    username: admin@163.com # 发送邮件的账号
    #    password: 123456 # 配置密码,注意,不是真正的密码,而是刚刚申请到的授权码
    #    default-encoding: utf-8 # 设置编码
    host: smtp.exmail.qq.com
    port: 465
    username: test@xxx.cn
    password: 123123
    default-encoding: utf-8 # 设置编码
    protocol: smtp
    properties: # 设置邮件超时时间防止服务器阻塞
      timeout: 5000
      connection-timeout: 5000
      write-timeout: 5000
      mail:
        debug: true # 表示开启 DEBUG 模式,这样,邮件发送过程的日志会在控制台打印出来,方便排查错误
        # 官方建议使用 465 端口,而 465 端口是 SSL 协议的,所以不仅要换端口,
        # 还需要进行 SSL 协议替换。下面是在 application.properties 进行的邮件发送相关配置,
        smtp:
          socketFactory:
            port: 465
          #socketFactoryClass: javax.net.ssl.SSLSocketFactory #配饰 SSL 加密工厂
          ssl:
            enable: true
  thymeleaf:
    cache: false
    mode: LEGACYHTML5 # 类型
    prefix: classpath:/templates/ # 模板存放的位置
    suffix: .html # 模板的后缀


2.3 创建EmailModel


用于封装邮件中需要包含的信息


package org.example.entity;

import java.io.Serializable;
import java.util.Map;

/**
 * @author test 2023-05-08 10:52:59
 */
public class EmailModel implements Serializable {
    /**
     * 收件人邮箱
     */
    private String[] recipientMailbox;

    /**
     * 邮件正文内容
     */
    private String content;

    /**
     * 邮件主题
     */
    private String title;

    /**
     * 抄送人邮箱
     */
    private String[] ccMailbox;

    /**
     * 加密抄送人邮箱
     */
    private String[] bccMailbox;

    /**
     * 真实名称/附件路径
     * enclosures: {“fileNmae”: "filePath+fileNmae"}
     */
    private Map enclosures;

    //    附件是否添加的到正文,默认false不添加
    //    private Boolean is_;


    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String[] getRecipientMailbox() {
        return recipientMailbox;
    }

    public void setRecipientMailbox(String[] recipientMailbox) {
        this.recipientMailbox = recipientMailbox;
    }

    public String[] getCcMailbox() {
        return ccMailbox;
    }

    public void setCcMailbox(String[] ccMailbox) {
        this.ccMailbox = ccMailbox;
    }

    public String[] getBccMailbox() {
        return bccMailbox;
    }

    public void setBccMailbox(String[] bccMailbox) {
        this.bccMailbox = bccMailbox;
    }

    public Map getEnclosures() {
        return enclosures;
    }

    public void setEnclosures(Map enclosures) {
        this.enclosures = enclosures;
    }
}


2.4 创建EmailService


package org.example.service;

import org.apache.commons.lang3.ArrayUtils;
import org.example.entity.EmailModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@EnableAsync
public class EmailService {
    Logger log = LoggerFactory.getLogger(getClass());

    /**
     * 将配置文件中的username注入到MAIL_USERNAME中, 这是发送人的邮箱地址
     */
    @Value("${spring.mail.username}")
    public String MAIL_USERNAME;

    /**
     * JavaMailSender类的对象, 是springboot自动装配的
     */
    @Resource
    private JavaMailSender javaMailSender;

    /**
     * template 模板引擎
     */
    @Autowired
    private TemplateEngine templateEngine;

    /**
     * 发送简单的邮件(内容为纯文本邮件)
     * @param model
     * @return
     */
    public boolean sendSimpleEmail(EmailModel model) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        // 设置发件人邮箱
        simpleMailMessage.setFrom(MAIL_USERNAME);

        // 设置接收人账号
        simpleMailMessage.setTo(model.getRecipientMailbox());

        // 设置邮件标题
        simpleMailMessage.setSubject(model.getTitle());

        // 设置抄送人
        String[] ccMailbox = model.getCcMailbox();
        if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
            simpleMailMessage.setCc(model.getCcMailbox());
        }

        // 加密抄送人邮箱
        String[] bccMailbox = model.getBccMailbox();
        if (ArrayUtils.isNotEmpty(bccMailbox)) {
            simpleMailMessage.setBcc(model.getBccMailbox()); // 加密抄送
        }

        // 发送日期
        simpleMailMessage.setSentDate(new Date());

        // 设置邮件正文内容
        simpleMailMessage.setText(model.getContent());

        // 发送邮件
        javaMailSender.send(simpleMailMessage);
        log.info("send simple email success!");
        return true;
    }

    /**
     * 发送富文本邮件(附件,图片,html等)
     *      使用MimeMessage来作为对象发送,
     *      但是邮件内容需要通过 MimeMessageHelper 对象来进行封装进MimeMessage 里
     * 注意:
     *      如果发送的内容包括html标签, 则需要 helper.setText(email.getContent(),true);
     *      第二个参数要为true,表示开启识别html标签.默认是false,也就是不识别.
     * @param model
     * @return
     */
    public boolean sendMimeEmail(EmailModel model) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);

        try {
            // 设置发件人邮箱
            helper.setFrom(MAIL_USERNAME);

            // 设置接收人账号
            helper.setTo(model.getRecipientMailbox());

            // 设置邮件标题
            helper.setSubject(model.getTitle());

            // 设置抄送人
            String[] ccMailbox = model.getCcMailbox();
            if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
                helper.setCc(model.getCcMailbox());
            }

            // 加密抄送人邮箱
            String[] bccMailbox = model.getBccMailbox();
            if (ArrayUtils.isNotEmpty(bccMailbox)) {
                helper.setBcc(model.getBccMailbox()); // 加密抄送
            }

            // 发送日期
            helper.setSentDate(new Date());

            // 发送的内容包括html标签, 需要 helper.setText(email.getContent(),true); 开启html识别
            // 使用 MimeMessage 对象, 调用setText方法, 里面的字符串是带有html标签的字符串,
            // 在发送邮件的时候会自动解析正文中的html标签
            helper.setText(model.getContent(), true);

            // 发送邮件
            javaMailSender.send(mimeMessage);
            log.info("send mime email success!");
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 使用 MimeMessage 发送富文本邮件(附件,图片,html等)
     * @param model
     * @return
     */
    public boolean sendAttachMimeEmail(EmailModel model) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        try {
            // 发送带附件的邮件, 需要加一个参数为true
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

            // 设置发件人邮箱
            helper.setFrom(MAIL_USERNAME);

            // 设置接收人账号
            helper.setTo(model.getRecipientMailbox());

            // 设置邮件标题
            helper.setSubject(model.getTitle());

            // 设置抄送人
            String[] ccMailbox = model.getCcMailbox();
            if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
                helper.setCc(model.getCcMailbox());
            }

            // 加密抄送人邮箱
            String[] bccMailbox = model.getBccMailbox();
            if (ArrayUtils.isNotEmpty(bccMailbox)) {
                helper.setBcc(model.getBccMailbox()); // 加密抄送
            }

            // 发送日期
            helper.setSentDate(new Date());

            // 发送的内容包括html标签, 需要 helper.setText(email.getContent(),true); 开启html识别
            // 使用 MimeMessage 对象, 调用setText方法, 里面的字符串是带有html标签的字符串,
            // 在发送邮件的时候会自动解析正文中的html标签
            helper.setText(model.getContent(), true);

            Map enclosures = model.getEnclosures();

            // 创建要传递的附件对象
            if (!CollectionUtils.isEmpty(enclosures)) { // import org.springframework.util.CollectionUtils;
                for (String key : enclosures.keySet()) {
                    // File file = new File("D:\mybatis.doc");
                    File file = new File((String) enclosures.get(key));

                    // 通过MimeMessageHelper对象的addAttachment方法来传递附件
                    // 第一个参数为附件名, 第二个参数为File对象
                    helper.addAttachment(key, file);
                }
            }

            // 预览文件
            // helper.addAttachment("2.jpg", new File("E:\\pic\\2.jpg"));
            // 配合前端可以直接预览图片
            // helper.addInline("p01", new FileSystemResource(new File("E:\\pic\\2.jpg")));
            // helper.addInline("p02", new FileSystemResource(new File("E:\\pic\\2.jpg")));

            // 发送邮件
            javaMailSender.send(mimeMessage);
            log.info("send mime email success!");
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 发送模板邮件 使用 thymeleaf 模板
     *   如果使用freemarker模板
     *       Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
     *       configuration.setClassForTemplateLoading(this.getClass(), "/templates");
     *       String emailContent = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("mail.ftl"), params);
     * @param model
     * @return
     */
    public boolean sendTemplateMail(EmailModel model) {
        // 发一个复杂一点的邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

            // 设置发件人邮箱
            helper.setFrom(MAIL_USERNAME);

            // 设置接收人账号
            helper.setTo(model.getRecipientMailbox());

            // 设置邮件标题
            helper.setSubject(model.getTitle());

            // 设置抄送人
            String[] ccMailbox = model.getCcMailbox();
            if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
                helper.setCc(model.getCcMailbox());
            }

            // 加密抄送人邮箱
            String[] bccMailbox = model.getBccMailbox();
            if (ArrayUtils.isNotEmpty(bccMailbox)) {
                helper.setBcc(model.getBccMailbox()); // 加密抄送
            }

            // 发送日期
            helper.setSentDate(new Date());

            // 使用模板thymeleaf
            // Context 是导这个包import org.thymeleaf.context.Context;
            Context context = new Context();
            // 定义模板数据
            context.setVariables(model.getEnclosures());
            // 获取thymeleaf的html模板, 指定模板路径
            // 在 Spring Boot 中, 模板引擎的默认配置已经将模板文件的根路径设置为 /src/main/resources/templates
            String emailContent = templateEngine.process("index.html", context);

            // 发送的内容包括html标签, 需要 helper.setText(email.getContent(),true); 开启html识别
            // 使用 MimeMessage 对象, 调用setText方法, 里面的字符串是带有html标签的字符串,
            // 发送邮件的时候会自动解析正文中的html标签
            helper.setText(emailContent, true);

            Map enclosures = model.getEnclosures();

            // 创建要传递的附件对象
            // import org.springframework.util.CollectionUtils;
            if (!CollectionUtils.isEmpty(enclosures)) {
                for (String key : enclosures.keySet()) {
                    // File file = new File(“D:\mybatis.doc”);
                    File file = new File((String)enclosures.get(key));
                    // 通过MimeMessageHelper对象的addAttachment方法来传递附件, 第一个参数为附件名, 第二个参数为FIle对象
                    helper.addAttachment(key, file);
                }
            }

            // 发送邮件
            javaMailSender.send(mimeMessage);
            log.info("send mime email success!");
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }
}


2.5 创建EmailController


package org.example.controller;

import org.example.entity.EmailModel;
import org.example.service.EmailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;

/**
 * 通知告警服务:
 *      集成 drools:
 *
 *      通知渠道: 站内信, 邮件, 短信, 微信 等等
 *      通知策略: 告警级别设置, 告警规则合并, 告警通知的周期。。 粒度
 *          交易通知:
 *              告警级别
 *              通知策略:
 *                  drools
 *          告警规则:mysql+springboot 交易服务 放在 同一台机器 --》 几百条 合并为一条
 *      权限,认证
 * @description:
 */
@RestController
public class EmailController {
    Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    EmailService emailService;

    @RequestMapping("test")
    public String test() {
        log.info("ok");
        return "ok";
    }

    /**
     * {
     *     "recipientMailbox": [
     *         "test@xxx.cn"
     *     ],
     *     "title": "简单测试主题",
     *     "content": "测试邮件测试人test"
     * }
     * @param model
     * @return
     * @throws MessagingException
     */
    @RequestMapping("sendSimpleEmail")
    public String sendSimpleEmail(@RequestBody EmailModel model) {
        emailService.sendSimpleEmail(model);
        return "success!";
    }

    /**
     * {
     *     "recipientMailbox": [
     *         "test@xxx.cn"
     *     ],
     *     "title": "html测试主题",
     *     "content": "

测试邮件测试人test

* 点击跳转百度 * " * } * @param model * @return * @throws MessagingException */ @RequestMapping("sendMimeEmail") public String sendMimeEmail(@RequestBody EmailModel model) { emailService.sendMimeEmail(model); return "success!"; } /** * { * "recipientMailbox": [ * "test@xxx.cn" * ], * "title": "附件测试主题", * "content": "", * "enclosures": { * "dog2.jpg": "E:\\img\\dog2.png" * } * } * @param model * @return */ @RequestMapping("sendAttachMimeEmail") public String sendAttachMimeEmail(@RequestBody EmailModel model) { emailService.sendAttachMimeEmail(model); return "success!"; } /** * { * "recipientMailbox": [ * "test@xxx.cn" * ], * "title": "thymeleaf 测试主题", * "enclosures": { * "dog2.jpg": "E:\\img\\dog2.png" * } * } * @param model * @return */ @RequestMapping("sendTemplateMail") @ResponseBody public String sendTemplateMail(@RequestBody EmailModel model) { emailService.sendTemplateMail(model); return "success!"; } }


3. 邮件发送类型讲解


下文中所有的实现都是写在 EmailService 类中。


3.1 发送简单的邮件(内容为纯文本邮件)


通过SimpleMailMessage对象来发送简单邮件,邮件的内容只能是纯文本,将内容封装到该对象中,再通过javaMailSender对象来发送邮件


/**
 * 发送简单的邮件(内容为纯文本邮件)
 * @param model
 * @return
 */
public boolean sendSimpleEmail(EmailModel model) {
	SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

	// 设置发件人邮箱
	simpleMailMessage.setFrom(MAIL_USERNAME);

	// 设置接收人账号
	simpleMailMessage.setTo(model.getRecipientMailbox());

	// 设置邮件标题
	simpleMailMessage.setSubject(model.getTitle());

	// 设置抄送人
	String[] ccMailbox = model.getCcMailbox();
	if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
		simpleMailMessage.setCc(model.getCcMailbox());
	}

	// 加密抄送人邮箱
	String[] bccMailbox = model.getBccMailbox();
	if (ArrayUtils.isNotEmpty(bccMailbox)) {
		simpleMailMessage.setBcc(model.getBccMailbox()); // 加密抄送
	}

	// 发送日期
	simpleMailMessage.setSentDate(new Date());

	// 设置邮件正文内容
	simpleMailMessage.setText(model.getContent());

	// 发送邮件
	javaMailSender.send(simpleMailMessage);
	log.info("send simple email success!");
	return true;
}


3.1.2 postman脚本


{
    "recipientMailbox": [
        "test@xxxx.cn"
    ],
    "title": "简单测试主题",
    "content": "

测试邮件测试人test

" }


3.1.3 发送请求,将email对象中的数据初始化



查看是否收到邮件,内容和我们发送的一样



3.2 发送简单的邮件(内容可以识别html标签)


3.2.1 方法1


直接和上面一样,使用SimpleMessage对象,调用setText方法,里面的字符串是带有html标签的字符串,在发送邮件的时候会自动解析正文中的html标签 代码和上面一样,只需要修改发送email的内容即可




3.2.2 方法2


使用MimeMessage来作为对象发送,但是邮件内容需要通过MimeMessageHelper对象来进行封装进MimeMessage里

注意:

如果发送的内容包括html标签,则需要 helper.setText(email.getContent(),true);第二个参数要为true,表示开启识别html标签.默认是false,也就是不识别.



/**
 * 发送富文本邮件(附件,图片,html等)
 *      使用MimeMessage来作为对象发送,
 *      但是邮件内容需要通过 MimeMessageHelper 对象来进行封装进MimeMessage 里
 * 注意:
 *      如果发送的内容包括html标签, 则需要 helper.setText(email.getContent(),true);
 *      第二个参数要为true,表示开启识别html标签.默认是false,也就是不识别.
 * @param model
 * @return
 */
public boolean sendMimeEmail(EmailModel model) {
	MimeMessage mimeMessage = javaMailSender.createMimeMessage();
	MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);

	try {
		// 设置发件人邮箱
		helper.setFrom(MAIL_USERNAME);

		// 设置接收人账号
		helper.setTo(model.getRecipientMailbox());

		// 设置邮件标题
		helper.setSubject(model.getTitle());

		// 设置抄送人
		String[] ccMailbox = model.getCcMailbox();
		if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
			helper.setCc(model.getCcMailbox());
		}

		// 加密抄送人邮箱
		String[] bccMailbox = model.getBccMailbox();
		if (ArrayUtils.isNotEmpty(bccMailbox)) {
			helper.setBcc(model.getBccMailbox()); // 加密抄送
		}

		// 发送日期
		helper.setSentDate(new Date());

		// 发送的内容包括html标签, 需要 helper.setText(email.getContent(),true); 开启html识别
		// 使用 MimeMessage 对象, 调用setText方法, 里面的字符串是带有html标签的字符串,
		// 在发送邮件的时候会自动解析正文中的html标签
		helper.setText(model.getContent(), true);

		// 发送邮件
		javaMailSender.send(mimeMessage);
		log.info("send mime email success!");
		return true;
	} catch (MessagingException e) {
		e.printStackTrace();
		return false;
	}
}


3.2.3 postman脚本


{
    "recipientMailbox": [
        "test@xxxx.cn"
    ],
    "title": "链接地址测试主题",
    "content": "点击跳转百度"
}




3.3 发送带静态资源(图片)的邮件中


方法和上面方法2中的代码是一致的,只相当于邮件内容还是html标签,现在就是使用的标签



3.3.1 postman脚本


{
    "recipientMailbox": [
        "test@xxxx.cn"
    ],
    "title": "图片测试主题",
    "content": ""
}


效果图:



3.4 发送带附件的邮件


我们的邮件中可能还需要附带一些附件,比如文件,或者jar包等等,如何发送附件呢? 也是使用MimeMessage对象和MimeMessageHelper对象,但是在创建MimeMessageHelper对象的时候需要加一个参数为true.



/**
 * 使用 MimeMessage 发送富文本邮件(附件,图片,html等)
 * @param model
 * @return
 */
public boolean sendAttachMimeEmail(EmailModel model) {
	MimeMessage mimeMessage = javaMailSender.createMimeMessage();

	try {
		// 发送带附件的邮件, 需要加一个参数为true
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

		// 设置发件人邮箱
		helper.setFrom(MAIL_USERNAME);

		// 设置接收人账号
		helper.setTo(model.getRecipientMailbox());

		// 设置邮件标题
		helper.setSubject(model.getTitle());

		// 设置抄送人
		String[] ccMailbox = model.getCcMailbox();
		if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
			helper.setCc(model.getCcMailbox());
		}

		// 加密抄送人邮箱
		String[] bccMailbox = model.getBccMailbox();
		if (ArrayUtils.isNotEmpty(bccMailbox)) {
			helper.setBcc(model.getBccMailbox()); // 加密抄送
		}

		// 发送日期
		helper.setSentDate(new Date());

		// 发送的内容包括html标签, 需要 helper.setText(email.getContent(),true); 开启html识别
		// 使用 MimeMessage 对象, 调用setText方法, 里面的字符串是带有html标签的字符串,
		// 在发送邮件的时候会自动解析正文中的html标签
		helper.setText(model.getContent(), true);

		Map enclosures = model.getEnclosures();

		// 创建要传递的附件对象
		if (!CollectionUtils.isEmpty(enclosures)) { // import org.springframework.util.CollectionUtils;
			for (String key : enclosures.keySet()) {
				// File file = new File("D:\mybatis.doc");
				File file = new File((String) enclosures.get(key));

				// 通过MimeMessageHelper对象的addAttachment方法来传递附件
				// 第一个参数为附件名, 第二个参数为File对象
				helper.addAttachment(key, file);
			}
		}

		// 预览文件
		// helper.addAttachment("2.jpg", new File("E:\\pic\\2.jpg"));
		// 配合前端可以直接预览图片
		// helper.addInline("p01", new FileSystemResource(new File("E:\\pic\\2.jpg")));
		// helper.addInline("p02", new FileSystemResource(new File("E:\\pic\\2.jpg")));

		// 发送邮件
		javaMailSender.send(mimeMessage);
		log.info("send mime email success!");
		return true;
	} catch (MessagingException e) {
		e.printStackTrace();
		return false;
	}
}


3.4.1 postman脚本


{
    "recipientMailbox": [
        "test@xxx.cn"
    ],
    "title": "附件测试主题",
    "content": "",
    "enclosures": {
        "dog2.jpg": "E:\\2.新员工\\img\\dog2.png"
    }
}


3.4.2 重点


  1. 第二个参数为true


MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
File file = new File(“D:\Users\Administrator\Desktop\picture\mybatis.doc”);


  1. 调用helper的addAttachment来添加附件


helper.addAttachment(file.getName(),file);



3.4.3 附件上传成功!



3.5 发送 thymleaf 模板邮件


3.5.1 模板 mail.html


放在resource/templates/mail目录下





    
    title


    

你看我, 哈哈哈!


3.5.2 postman脚本


{
    "recipientMailbox": [
        "langpf@houputech.cn"
    ],
    "title": "thymeleaf 测试主题",
    "enclosures": {
        "dog2.jpg": "E:\\2.新员工\\img\\dog2.png"
    }
}


/**
 * 发送模板邮件 使用 thymeleaf 模板
 *   如果使用freemarker模板
 *       Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
 *       configuration.setClassForTemplateLoading(this.getClass(), "/templates");
 *       String emailContent = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("mail.ftl"), params);
 * @param model
 * @return
 */
public boolean sendTemplateMail(EmailModel model) {
	// 发一个复杂一点的邮件
	MimeMessage mimeMessage = javaMailSender.createMimeMessage();

	try {
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

		// 设置发件人邮箱
		helper.setFrom(MAIL_USERNAME);

		// 设置接收人账号
		helper.setTo(model.getRecipientMailbox());

		// 设置邮件标题
		helper.setSubject(model.getTitle());

		// 设置抄送人
		String[] ccMailbox = model.getCcMailbox();
		if (ArrayUtils.isNotEmpty(ccMailbox)) { // org.apache.commons.lang3.ArrayUtils
			helper.setCc(model.getCcMailbox());
		}

		// 加密抄送人邮箱
		String[] bccMailbox = model.getBccMailbox();
		if (ArrayUtils.isNotEmpty(bccMailbox)) {
			helper.setBcc(model.getBccMailbox()); // 加密抄送
		}

		// 发送日期
		helper.setSentDate(new Date());

		// 使用模板thymeleaf
		// Context 是导这个包import org.thymeleaf.context.Context;
		Context context = new Context();
		// 定义模板数据
		context.setVariables(model.getEnclosures());
		// 获取thymeleaf的html模板, 指定模板路径
		// 在 Spring Boot 中, 模板引擎的默认配置已经将模板文件的根路径设置为 /src/main/resources/templates
		String emailContent = templateEngine.process("index.html", context);

		// 发送的内容包括html标签, 需要 helper.setText(email.getContent(),true); 开启html识别
		// 使用 MimeMessage 对象, 调用setText方法, 里面的字符串是带有html标签的字符串,
		// 发送邮件的时候会自动解析正文中的html标签
		helper.setText(emailContent, true);

		Map enclosures = model.getEnclosures();

		// 创建要传递的附件对象
		// import org.springframework.util.CollectionUtils;
		if (!CollectionUtils.isEmpty(enclosures)) {
			for (String key : enclosures.keySet()) {
				// File file = new File(“D:\mybatis.doc”);
				File file = new File((String)enclosures.get(key));
				// 通过MimeMessageHelper对象的addAttachment方法来传递附件, 第一个参数为附件名, 第二个参数为FIle对象
				helper.addAttachment(key, file);
			}
		}

		// 发送邮件
		javaMailSender.send(mimeMessage);
		log.info("send mime email success!");
		return true;
	} catch (MessagingException e) {
		e.printStackTrace();
		return false;
	}
}