springboot整合邮箱功能二 html邮件

1. 准备工作

1.1 qq邮箱设置

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

 

SpringBoot项目配置

2.1 springboot项目中引入email依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <!--<version>2.7.11</version>-->
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <project-name>spring-boot-email</project-name>

        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <groupId>org.example</groupId>
    <artifactId>${project-name}</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>${project-name}</name>
    <description>${project-name}</description>

    <dependencies>
        <!--发送邮件的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- ================= 应用 ================== -->
        <!-- thymeleaf模板 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.25</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-android</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.1</version>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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<String, Object> 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<String, Object> getEnclosures() {
        return enclosures;
    }

    public void setEnclosures(Map<String, Object> 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<String, Object> 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<String, Object> 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": "<h1>测试邮件测试人test</h1>
     *        <a href=\"https://www.baidu.com/\">点击跳转百度</a>
     *        <img src=\"https://img-blog.csdnimg.cn/2092623a09ed4d19ac4b9ab5301462cf.png\"></img>"
     * }
     * @param model
     * @return
     * @throws MessagingException
     */
    @RequestMapping("sendMimeEmail")
    public String sendMimeEmail(@RequestBody EmailModel model) {
        emailService.sendMimeEmail(model);
        return "success!";
    }

    /**
     * {
     *     "recipientMailbox": [
     *         "test@xxx.cn"
     *     ],
     *     "title": "附件测试主题",
     *     "content": "<img src=\"https://img-blog.csdnimg.cn/2092623a09ed4d19ac4b9ab5301462cf.png\"></img>",
     *     "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": "<h1>测试邮件测试人test</h1>"
}

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": "<a href=\"https://www.baidu.com/\">点击跳转百度</a>"
}

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

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

3.3.1 postman脚本

{
    "recipientMailbox": [
        "test@xxxx.cn"
    ],
    "title": "图片测试主题",
    "content": "<img src=\"https://img-blog.csdnimg.cn/2092623a09ed4d19ac4b9ab5301462cf.png\"></img>"
}

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<String, Object> 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": "<img src=\"https://img-blog.csdnimg.cn/2092623a09ed4d19ac4b9ab5301462cf.png\"></img>",
    "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目录下

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>title</title>
</head>
<body>
    <h3>你看我<span style="font-size: 35px" th:text="${username}"></span>, 哈哈哈!</h3>
</body>
</html>

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<String, Object> 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;
	}
} 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值