当前位置:   article > 正文

SpringBoot之发送邮件五种简单示例_spootboot发送邮件

spootboot发送邮件

1.引入pom依赖

  • spring-boot-starter-mail : 通过此依赖进行邮件发送
  • spring-boot-starter-thymeleaf : thymeleaf 邮件模板依赖
  • spring-boot-starter-freemarker : freemarker 邮件模板依赖
    <!--引入springboot父工程依赖-->
    <!--引入依赖作用:
    可以省去version标签来获得一些合理的默认配置
    -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>

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

        <!--两个用来做测试的jar包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

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

        <!--freemarker 邮件模板-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39


2.application.yml配置

  • 我这里用的qq邮箱测试
  •    注意邮箱的 host 和 port
spring:
  mail:
    host: smtp.qq.com            # SMTP服务器主机(我这里使用qq发送 所以 smtp主机就是qq)
    port: 587                    # QQ邮箱 SMTP port号:25   (使用SSL时,port号465或587)如果使用端口为465,将protocol的smtp改为smtps
    username: 1326303027@qq.com  # 发件人的邮箱
    password: xnkdngXXXXXXXd   # QQ邮箱的授权码(网上搜一下 qq怎么获取自己的SMTP授权码 就行 有好多攻略)
    default-encoding: UTF-8      # 默认MimeMessage编码
    properties:                  # 其他JavaMail会话属性。
      mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory #配饰 SSL 加密工厂
    test-connection: true        #是否在启动时测试邮件服务器是否可用
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10


3.获取qq授权码

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述



4.配置启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @创建人: Mhh
 * @创建时间: 2022/3/14
 * @描述: 发送邮件启动类
 */
@SpringBootApplication
public class MailSpringApplication {

    public static void main(String[] args) {
        SpringApplication.run(MailSpringApplication.class,args);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16


5.发送邮件测试


5.1 发送简单的邮件

  • 发件人必须是配在yml里的spring.mail.username的值;
  • 密送的邮箱 : 会把邮件秘密抄送给指定的秘密邮箱;
    在这里插入图片描述

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @创建人: Mhh
 * @创建时间: 2022/3/14
 * @描述: 发送邮件
 */

@SpringBootTest
@RunWith(SpringRunner.class)
public class MailTest {

    /*
    * 发送邮件对象
    * */
    @Autowired
    private JavaMailSender javaMailSender;

    /*
    * thymeleaf html渲染对象
    * */
    @Autowired
    private TemplateEngine templateEngine;

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


    /*
    * 发送简单的邮件
    * */
    @Test
    public void sendSimpleMail() {
        SimpleMailMessage smm = new SimpleMailMessage();
        // 主题
        smm.setSubject("发送简单邮件主题");
        // 发件人的邮箱
//        smm.setFrom(from);
        smm.setFrom("1326303027@qq.com");
        // 发送日期
        smm.setSentDate(new Date(1646100707000L));//2022-03-01 10:11:47
        // 要发给的邮箱(收件人)
        smm.setTo("1326303027@qq.com");
        // 抄送邮箱
        smm.setCc("1326303027@qq.com");
        // 邮件内容
        smm.setText("简单邮件正文\n 您好,今天又是美好的一天");
        // 快速回复
        smm.setReplyTo("1326303027@qq.com");
        // 密送的邮箱
        smm.setBcc("1326303027@qq.com");
        // 发送邮件
        javaMailSender.send(smm);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81


5.2 发送邮件携带附件

  • 创建 MimeMessageHelper对象时 入参multipart 必须为true;
    在这里插入图片描述
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @创建人: Mhh
 * @创建时间: 2022/3/14
 * @描述: 发送邮件
 */

@SpringBootTest
@RunWith(SpringRunner.class)
public class MailTest {

    /*
    * 发送邮件对象
    * */
    @Autowired
    private JavaMailSender javaMailSender;

    /*
    * thymeleaf html渲染对象
    * */
    @Autowired
    private TemplateEngine templateEngine;

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

    /*
    * 发送邮件携带附件
    * */
    @Test
    public void sendAttachFileMail() throws MessagingException, IOException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //这里携带附件(入参 multipart 必须为true)
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        // 主题
        helper.setSubject("发送邮件携带附件");
        // 邮件内容
        helper.setText("邮件正文\n 此邮件 携带附件 请查收");
        // 发件人的邮箱
//        smm.setFrom(from);
        helper.setFrom("1326303027@qq.com");
        // 发送日期
        helper.setSentDate(new Date());
        // 要发给的邮箱
        helper.setTo("1326303027@qq.com");
        // 密送的邮箱
        helper.setBcc("1326303027@qq.com");
        // 抄送邮箱
        helper.setCc("1326303027@qq.com");
        // 快速回复
        helper.setReplyTo("1326303027@qq.com");
        // 附件(多个 就addAttachment()多个)
        FileSystemResource resource = new FileSystemResource(new File("D:\\采购需求单.xls"));
        helper.addAttachment(
                Objects.requireNonNull(resource.getFilename()),//附件名称
                resource,  //附件流
                Files.probeContentType(Paths.get(Objects.requireNonNull(resource.getFilename()))));//附件类型
        helper.addAttachment(
                Objects.requireNonNull(resource.getFilename()),//附件名称
                resource,//附件流
                Files.probeContentType(Paths.get(Objects.requireNonNull(resource.getFilename()))));//附件类型

        //发送邮件
        javaMailSender.send(mimeMessage);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92


5.3 发送邮件 图片内嵌式

  • 内嵌式 : 只能是图片
  • 邮件内容 : 配置html渲染 所以入参要填true
            图片内嵌 邮件内容的 cid:{值}要和addInLine里的入参 contentId一致

在这里插入图片描述

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @创建人: Mhh
 * @创建时间: 2022/3/14
 * @描述: 发送邮件
 */

@SpringBootTest
@RunWith(SpringRunner.class)
public class MailTest {

    /*
    * 发送邮件对象
    * */
    @Autowired
    private JavaMailSender javaMailSender;

    /*
    * thymeleaf html渲染对象
    * */
    @Autowired
    private TemplateEngine templateEngine;

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

    /*
    * 发送邮件  图片内嵌式
    * */
    @Test
    public void sendImgResMail() throws MessagingException, IOException {

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //这里携带附件(入参 multipart 必须为true)
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        // 主题
        helper.setSubject("图片内嵌式邮件主题");
        // 发件人的邮箱
//        smm.setFrom(from);
        helper.setFrom("1326303027@qq.com");
        // 发送日期
        helper.setSentDate(new Date());
        // 要发给的邮箱
        helper.setTo("1326303027@qq.com");
        // 密送的邮箱
        helper.setBcc("1326303027@qq.com");
        // 抄送邮箱
        helper.setCc("1326303027@qq.com");
        // 快速回复
        helper.setReplyTo("1326303027@qq.com");
        // 邮件内容(html渲染 所以要填true)
        helper.setText("<p>hello 大家好,这是一封测试邮件,这封邮件包含两种图片,分别如下</p><p>第一张图片:</p><img src='cid:aa'/><p>第二张图片:</p><img src='cid:bb'/>",true);
        // 图片内嵌   邮件内容的 cid:{值}要和addInLine里的入参 contentId一致
        FileSystemResource aaResource = new FileSystemResource(new File("D:\\aa.png"));
        FileSystemResource bbResource = new FileSystemResource(new File("D:\\bb.jpg"));
        helper.addInline("aa", aaResource);
        helper.addInline("bb", bbResource);
        //发送邮件
        javaMailSender.send(mimeMessage);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86


5.4 发送邮件 使用 Thymeleaf 做邮件模板


5.4.1 引入 Thymeleaf 邮件模板Pom依赖

        <!--thymeleaf 邮件模板-->
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

5.4.2 编写一个简单的Thymeleaf邮件模板 thymeleaf.html,放到resources/templates

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf: 放假通知</title>
</head>
<body>
<div>各位小伙伴们:</div><br>
<div><span style="color: red" th:text="${festival}"></span>即将到来,在此祝大家节日快乐!</div>
<div>根据公司安排将于<span style="color: #ff0000" th:text="${date}"></span>放假,请知悉。</div>
<p style="color: blue">内嵌图片:</p>
<br/>
<div><img src='cid:aa'/></div>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

5.4.3 发送邮件 使用 Thymeleaf 做邮件模板(携带内嵌图片+附件)测试

  • 通过 Context 邮件模板进行配置需要参数
  • 其中festival、date是模板参数,需要发送邮件时传给模板(模板参数自己随便定义 只要相互对应起来)

在这里插入图片描述

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @创建人: Mhh
 * @创建时间: 2022/3/14
 * @描述: 发送邮件
 */

@SpringBootTest
@RunWith(SpringRunner.class)
public class MailTest {

    /*
    * 发送邮件对象
    * */
    @Autowired
    private JavaMailSender javaMailSender;

    /*
    * thymeleaf html渲染对象
    * */
    @Autowired
    private TemplateEngine templateEngine;

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

    /*
    * Thymeleaf邮件模板发送邮件(携带内嵌图片+附件):注意 FreemarkerMail与Thymeleaf的html写法不一样
    * */
    @Test
    public void sendThymeleafMail() throws MessagingException, IOException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        // 主题
        helper.setSubject("Thymeleaf邮件模板 邮件主题");
        // 发件人的邮箱
//        smm.setFrom(from);
        helper.setFrom("1326303027@qq.com");
        // 发送日期
        helper.setSentDate(new Date());
        // 要发给的邮箱
        helper.setTo("1326303027@qq.com");
        // 密送的邮箱
        helper.setBcc("1326303027@qq.com");
        // 抄送邮箱
        helper.setCc("1326303027@qq.com");
        // 快速回复
        helper.setReplyTo("1326303027@qq.com");
        //开始配置FreeMarker邮件模板
        Context context = new Context();
        //FreeMarker邮件模板参数
        context.setVariable("festival", "春节");
        context.setVariable("date", "2022-01-01");
        //thymeleaf.html:映射的html名字
        String process = templateEngine.process("thymeleaf.html", context);
        //邮件内容(html渲染 所以要填true)
        helper.setText(process, true);
        //内嵌图片
        FileSystemResource aaResource = new FileSystemResource(new File("D:\\aa.png"));
        // 内嵌的名字 'aa'要和 html里img标签中cid:{值}一致
        helper.addInline("aa", aaResource);

        // 附件(多个 就addAttachment()多个)
        FileSystemResource resource = new FileSystemResource(new File("D:\\采购需求单.xls"));
        helper.addAttachment(
                Objects.requireNonNull(resource.getFilename()),//附件名称
                resource,  //附件流
                Files.probeContentType(Paths.get(Objects.requireNonNull(resource.getFilename()))));//附件类型
        // 发送邮件
        javaMailSender.send(mimeMessage);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99


5.5 发送邮件 使用 Freemarker 做邮件模板


5.5.1 引入 Freemarker 邮件模板Pom依赖

        <!--freemarker 邮件模板-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

5.5.2 编写一个简单的Freemarker邮件模板 freemarker.html,放到resources/templates

  • 这里的和thymeleaf的区别在于 freemarker可以放在自己随便的定义的包下(只要在代码中指定哪个包下就行)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Freemarker: 放假通知</title>
</head>
<body>
<div>各位小伙伴们:</div><br>
<div><span style="color: red">${festival}</span>即将到来,在此祝大家节日快乐!</div>
<div>根据公司安排将于<span style="color: red">${date}</span>放假,请知悉。</div>
<p style="color: green">内嵌图片:</p>
<br/>
<div><img src='cid:aa'/></div>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

5.5.3 发送邮件 使用 Freemarker 做邮件模板(携带内嵌图片+附件)测试

  • 通过 Configuration 邮件模板进行配置需要参数以及模板位置和模板名称

在这里插入图片描述

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @创建人: Mhh
 * @创建时间: 2022/3/14
 * @描述: 发送邮件
 */

@SpringBootTest
@RunWith(SpringRunner.class)
public class MailTest {

    /*
    * 发送邮件对象
    * */
    @Autowired
    private JavaMailSender javaMailSender;

    /*
    * thymeleaf html渲染对象
    * */
    @Autowired
    private TemplateEngine templateEngine;

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

    /*
    *
    * FreemarkerMail 邮件模板发送邮件(带内嵌图片): 注意 FreemarkerMail与Thymeleaf的html写法不一样
    * */
    @Test
    public void sendFreemarkerMail() throws MessagingException, IOException, TemplateException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 因为要携带附件所以为true
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        // 主题
        helper.setSubject("FreemarkerMail 邮件模板 邮件主题");
        // 发件人的邮箱
//        smm.setFrom(from);
        helper.setFrom("1326303027@qq.com");
        // 发送日期
        helper.setSentDate(new Date());
        // 要发给的邮箱
        helper.setTo("1326303027@qq.com");
        // 密送的邮箱
        helper.setBcc("1326303027@qq.com");
        // 抄送邮箱
        helper.setCc("1326303027@qq.com");
        // 快速回复
        helper.setReplyTo("1326303027@qq.com");
        //构建 Freemarker 的基本配置
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
        // 配置模板位置
        ClassLoader loader = this.getClass().getClassLoader();
        //模板的位置  在templates包下
        configuration.setClassLoaderForTemplateLoading(loader, "templates");
        //加载模板(入参具体模板名称)
        Template template = configuration.getTemplate("freemarker.html");
        //模板参数
        Map<String,Object> map = new HashMap();//可以自定义自己的对象
        map.put("date","2022-05-01");
        map.put("festival","劳动节");
        StringWriter out = new StringWriter();
        //模板渲染,渲染的结果将被保存到 out 中 ,将out 中的 html 字符串发送即可
        template.process(map, out);
        //邮件内容(html渲染 所以要填true)
        helper.setText(out.toString(), true);
        //内嵌图片
        FileSystemResource aaResource = new FileSystemResource(new File("D:\\aa.png"));
        // 内嵌的名字 'aa'要和 html里img标签中cid:{值}一致
        helper.addInline("aa", aaResource);

        // 附件(多个 就addAttachment()多个)
        FileSystemResource resource = new FileSystemResource(new File("D:\\采购需求单.xls"));
        helper.addAttachment(
                Objects.requireNonNull(resource.getFilename()),//附件名称
                resource,  //附件流
                Files.probeContentType(Paths.get(Objects.requireNonNull(resource.getFilename()))));//附件类型
        // 发送邮件
        javaMailSender.send(mimeMessage);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107







链接:SpringBoot之发送邮件五种简单示例 源代码下载地址

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号