当前位置:   article > 正文

springboot javaMailSender 发送带附件的模板邮件_javamail 邮件模板

javamail 邮件模板

经常遇到需要发送带附件的邮件的功能,特写一份详细的笔记

1、老样子,首先引入依赖

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

<!--thymeleaf 邮件模板依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.5.4</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2、编写一份邮件的html模板

邮件模板放在 resource/templates路径下

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>template</title>
    <style>
        p {
            font-size: larger;
            font-weight: bold;
        }
        #yes {
            padding: 10px;
            font-size: 16px;
            border: none;
            border-radius: 5px;
            background-color: orange;
            color: white;
        }
        #no {
            padding: 10px;
            font-size: 16px;
            border: none;
            border-radius: 5px;
            background-color: darkgrey;
            color: white;
        }
        div {
            width: 100%;
            margin: 0 auto;
        }
        div.offer-info {
            padding: 10px 10px 0;
            border-top: 1px solid #ccc;
            color: #747474;
            margin-bottom: 20px;
            line-height: 1.3em;
            font-size: 12px;
        }
        #note,#note1{
            font-weight: bold;
        }
    </style>
</head>
<body>
<p>XXXXXX</p>

<span th:text="${userName}"></span>,您好
<br>
<br>
<div>请查收XXXXXX</div><br>
<span id="note">您的XXXXXX</span><br><br>
<span id="note1" style="color: orange">请点击XXXX</span><br><br>


<button id="no">A</button>
<button id="yes">B</button>
<br>
<br>
<div class="offer-info">
    <br>
    <p>此为系统邮件,请勿回复<br>Please do not reply to this system email</p>
</div>
</body>
</html>
  • 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

3、邮件配置

offerMail:
  host: mail.XXXXX.com
  port: 25
  sender: XXXXXX@XXXX.com
  password: XXXXXXXX
  • 1
  • 2
  • 3
  • 4
  • 5

4、上代码


@Service
public class Template {

   	@Autowired
    private TemplateEngine templateEngine;

    private JavaMailSenderImpl javaMailSender = null;


	public void sendThymeleafMail() throws Exception {
        JavaMailSender javaMailSender = getJavaMailSenderParam();
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject("邮件标题");
        helper.setFrom("XXXXX@XX.com");
        helper.setTo("XXXX@qq.com");
        helper.setSentDate(new Date());

        // 绑定到mail.thymeleaf模板的数据
        Context context = new Context();
        context.setVariable("userName", "Tom");
        String process = templateEngine.process("mail.html", context);
        // setText()第二个参数:开启html模板
        helper.setText(process, true);

        // 添加附件 TODO
        File emailFile = new File();
        helper.addAttachment("XXXX.PDF", emailFile);
        javaMailSender.send(mimeMessage);
    }
}

    private JavaMailSender getJavaMailSenderParam() {
        if (javaMailSender != null) {
            return javaMailSender;
        }
        System.getProperties().setProperty("mail.mime.splitlongparameters", "false");

        javaMailSender = new JavaMailSenderImpl();

        Properties properties = new Properties();
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        javaMailSender.setJavaMailProperties(properties);
        javaMailSender.setPort(appConfig.getOfferMail().getPort());
        javaMailSender.setUsername(appConfig.getOfferMail().getSender());
        javaMailSender.setPassword(appConfig.getOfferMail().getPassword());
        javaMailSender.setDefaultEncoding("UTF-8");
        javaMailSender.setHost(appConfig.getOfferMail().getHost());
        return javaMailSender;
    }
  • 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

搞定

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/591799
推荐阅读
相关标签
  

闽ICP备14008679号