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

关键的Maven依赖

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.2.RELEASEversion>
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-mailartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>com.icegreengroupId>
            <artifactId>greenmailartifactId>
            <version>1.5.0version>
            <scope>testscope>
        dependency>
    dependencies>

application.yml(以126邮箱为例)

spring:
  mail:
    host: smtp.126.com
    port: 25
    protocol: smtp
    defaultEncoding: UTF-8
    username: [email protected]
    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("[email protected]");
        helper.setSubject("邮件主题");
        // 若发送HTML格式,第二个参数必须是true 
        helper.setText(textHtml, true);
        helper.setTo("[email protected]");
        helper.addAttachment("鲁棒图.jpg", new File("D:\\鲁棒图.jpg"));
        helper.addAttachment("鲁棒图原则.jpg", new File("D:\\鲁棒图原则.jpg"));
        javaMailSender.send(mimeMessage);
        return "Sent";
    }
}

templates/emails/welcome.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>Emailtitle>
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/

你可能感兴趣的:(SpringBoot)