Spring Boot 整合郵件服務

2023-05-02 12:00:16

參考教學

首先參考了 Spring Boot整合郵件設定,這篇文章寫的很好,按照上面的操作一步步走下去就行了。

遇到的問題

版本設定

然後因為反覆設定版本很麻煩,所以參考了 如何統一引入 Spring Boot 版本?

FreeMarker

在設定 FreeMarker 時,發現找不到 FreeMarkerConfigurer 類,參考了 springboot整合Freemark模板(詳盡版) 發現要新增 web 模組。

測試註解

在使用測試類的時候,我只新增了 @SpringBootTest 註解,報空指標,參考了 測試類的@RunWith與@SpringBootTest註解 發現還要新增 @RunWith(SpringRunner.class) 註解。

實踐結果

程式碼地址

完成的專案地址

核心程式碼

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>fun.seolas</groupId>
    <artifactId>spring-boot-mail-demo</artifactId>
    <version>1.0-SNAPSHOT</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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
application.yaml
spring:
  mail:
    host: smtp.qq.com #傳送郵件伺服器
    username: [email protected] #QQ郵箱
    password: xxx #使用者端授權碼
    protocol: smtp #傳送郵件協定
    properties.mail.smtp.auth: true
    properties.mail.smtp.port: 465 #埠號465或587
    properties.mail.display.sendmail: aaa #可以任意
    properties.mail.display.sendname: bbb #可以任意
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true #開啟SSL
    default-encoding: utf-8
  freemarker:
    cache: false # 快取設定 開發階段應該設定為false 因為經常會改
    suffix: .html # 模版字尾名 預設為ftl
    charset: UTF-8 # 檔案編碼
    template-loader-path: classpath:/templates/  # 存放模板的資料夾,以resource資料夾為相對路徑

my:
  toemail: [email protected]
MailService.java
package fun.seolas;

import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Service
public class MailService {
    // Spring官方提供的整合郵件服務的實現類,目前是Java後端傳送郵件和整合郵件服務的主流工具。
    @Resource
    private JavaMailSender mailSender;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    // 從組態檔中注入發件人的姓名
    @Value("${spring.mail.username}")
    private String fromEmail;

    /**
     * 傳送文字郵件
     *
     * @param to      收件人
     * @param subject 標題
     * @param content 正文
     */
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail); // 發件人
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    /**
     * 傳送html郵件
     */
    public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
        //注意這裡使用的是MimeMessage
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(fromEmail);
        helper.setTo(to);
        helper.setSubject(subject);
        //第二個引數:格式是否為html
        helper.setText(content, true);
        mailSender.send(message);
    }

    /**
     * 傳送freemarker郵件
     */
    public void sendTemplateMail(String to, String subject, String templatehtml) throws Exception {
        // 獲得模板
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templatehtml);
        // 使用Map作為資料模型,定義屬性和值
        Map<String, Object> model = new HashMap<>();
        model.put("myname", "Seolas");
        // 傳入資料模型到模板,替代模板中的預留位置,並將模板轉化為html字串
        String templateHtml = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
        // 該方法本質上還是傳送html郵件,呼叫之前傳送html郵件的方法
        this.sendHtmlMail(to, subject, templateHtml);
    }

    /**
     * 傳送帶附件的郵件
     */
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        //要帶附件第二個引數設為true
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(fromEmail);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);

        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        helper.addAttachment(fileName, file);

        mailSender.send(message);
    }
}

MailTest.java
package fun.seolas;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;

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

    @Autowired
    private MailService mailService;

    @Value("${my.toemail}")
    private String toemail;

    @Test
    public void test01() {
        mailService.sendSimpleMail(toemail, "普通文字郵件", "普通文字郵件內容");
    }

    @Test
    public void test02() throws MessagingException {
        mailService.sendHtmlMail(toemail, "一封html測試郵件",
                "<div style=\"text-align: center;position: absolute;\" >\n"
                        + "<h3>\"一封html測試郵件\"</h3>\n"
                        + "<div>一封html測試郵件</div>\n"
                        + "</div>");
    }

    @Test
    public void test3() throws Exception {
        mailService.sendTemplateMail(toemail, "基於模板的html郵件", "freemarkertemp.html");
    }

    @Test
    public void test04() throws MessagingException {
        String filePath = "C:\\Users\\Julia\\Downloads\\測試.txt";
        mailService.sendAttachmentsMail(toemail, "帶附件的郵件", "郵件中有附件", filePath);
    }

}