基于 SpringBoot、Thymeleaf 发送 HTML 格式的邮件,带附件

关键的Maven依赖
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.icegreen</groupId>
            <artifactId>greenmail</artifactId>
            <version>1.5.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
application.yml(以126邮箱为例)
spring:
  mail:
    host: smtp.126.com
    port: 25
    protocol: smtp
    defaultEncoding: UTF-8
    username: YOUR_EMAIL@126.com
    password: YOUR_PASSWORD
核心代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class EmailService {
    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private JavaMailSender javaMailSender;

    public String sendMail() throws MessagingException {
        Map<String, Object> variables = new HashMap<>();
        variables.put("name", "小明");
        variables.put("age", 28);
        Context context = new Context();
        context.setVariables(variables);

        String textHtml = templateEngine.process("emails/welcome", context);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 若发生附件,第二个参数必须是true
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        // 这里一定要和配置文件里的username值一样才行
        helper.setFrom("YOUR_EMAIL@126.com");
        helper.setSubject("邮件主题");
        // 若发送HTML格式,第二个参数必须是true 
        helper.setText(textHtml, true);
        helper.setTo("some@qq.com");
        helper.addAttachment("鲁棒图.jpg", new File("D:\\鲁棒图.jpg"));
        helper.addAttachment("鲁棒图原则.jpg", new File("D:\\鲁棒图原则.jpg"));
        javaMailSender.send(mimeMessage);
        return "Sent";
    }
}

templates/emails/welcome.html

<!doctype html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Email</title>
</head>
<body>
    <div>Welcome <b th:text="${name}"></b></div>
    <div> Your age is <b th:text="${age}"></b></div>
</body>
</html>
参考资料
  • https://mkyong.com/spring-boot/spring-boot-how-to-send-email-via-smtp/
  • http://dolszewski.com/spring/sending-html-mail-with-spring-boot-and-thymeleaf/
  • https://springhow.com/spring-boot-email-thymeleaf/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值