一、引入依赖

<!-- 邮箱依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 邮箱模板 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

二、邮箱授权

开启邮箱服务

image-20210614020144012

开通服务

image-20210614020903719

三、配置文件

spring:
  application:
    name: test-service

  # 邮箱配置
  mail:
    host: smtp.qq.com
    username: 549684996@qq.com
    # 邮箱授权码
    password: 这里填申邮箱授权码
    default-encoding: UTF-8
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
  freemarker:
    # 缓存配置,开发阶段应该配置为false,因为经常会改
    cache: false
    # 模版后缀名,默认为ftl
    suffix: .html
    # 文件编码
    charset: UTF-8
    # 存放模板的文件夹,以resource文件夹为相对路径
    template-loader-path: classpath:/templates/

四、测试发送

4.1 简单邮件

测试代码

package com.qiang.test.service;

/**
 * @author: 小强崽
 * @create: 2021-06-14 02:14
 * @description: 邮件测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class EmailTest {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Test
    public void test1() {
        // 简单邮件
        SimpleMailMessage message = new SimpleMailMessage();
        // 发送方
        message.setFrom("549684996@qq.com");
        // 接收方
        message.setTo("875667601@qq.com");
        // 邮件主题
        message.setSubject("测试简单邮件主题");
        // 邮件内容
        message.setText("测试简单邮件内容");
        try {
            javaMailSender.send(message);
            log.info("发送简单邮件成功");
        } catch (MailException e) {
            e.printStackTrace();
            log.error("发送简单邮件失败");
        }
    }
}

发送简单邮件成功

image-20210614024432222

接收简单邮件成功

image-20210614024410738

4.2 复杂邮件

测试代码

package com.qiang.test.service;

/**
 * @author: 小强崽
 * @create: 2021-06-14 02:14
 * @description: 邮件测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class EmailTest {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Test
    public void test2() throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setFrom("549684996@qq.com");
        messageHelper.setTo("875667601@qq.com");
        messageHelper.setSubject("测试复杂邮件主题");
        // 内嵌图片,资源名称 photo 必须与 text 正文中 cid:photo 对应起来
        messageHelper.setText("<html><body><img src=\"cid:photo\" ></body></html>", true);
        FileSystemResource file = new FileSystemResource(new File("D:/File/Download/photo.jpg"));
        // 这里需要注意的是addInline函数中资源名称photo需要与正文中cid:photo对应起来
        messageHelper.addInline("photo", file);
        // 添加附件
        messageHelper.addAttachment("测试复杂邮件附件1.docx", new File("D:/File/Download/测试复杂邮件附件1.docx"));
        messageHelper.addAttachment("测试复杂邮件附件2.docx", new File("D:/File/Download/测试复杂邮件附件2.docx"));
        try {
            javaMailSender.send(mimeMessage);
            log.info("发送复杂邮件成功");
        } catch (MailException e) {
            e.printStackTrace();
            log.error("发送复杂邮件失败");
        }
    }
}

发送复杂邮件成功

image-20210614032503528

接收复杂邮件成功

image-20210614032558337

4.3 模板邮件

测试代码

package com.qiang.test.service;

/**
 * @author: 小强崽
 * @create: 2021-06-14 02:14
 * @description: 邮件测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class EmailTest {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Test
    public void test3() throws MessagingException, IOException, TemplateException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        messageHelper.setFrom("549684996@qq.com");
        messageHelper.setTo("875667601@qq.com");
        messageHelper.setSubject("测试模板邮件主题");
        // 获取模板
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate("mailTemplate.html");
        // 使用Map作为数据模型,定义属性和值
        Map<String, Object> model = new HashMap<>();
        model.put("name", "小强崽");
        model.put("username", "admin");
        model.put("password", "123456");
        // 传入数据模型到模板,替代模板中的占位符,并将模板转化为html字符串
        String templateHtml = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
        messageHelper.setText(templateHtml, true);
        try {
            javaMailSender.send(mimeMessage);
            log.info("发送模板邮件成功");
        } catch (MailException e) {
            e.printStackTrace();
            log.error("发送模板邮件失败");
        }
    }
}

准备模板

mailTemplate.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试模板邮件</title>
</head>
<body>
<h1>测试模板邮件标题</h1>
<div>名字:${name}</div>
<div>账号:${username}</div>
<div>密码:${password}</div>
</body>
</html>

发送模板邮件成功

image-20210614041037488

接收模板邮件成功

image-20210614041109640