在Spirng Boot中使用Thymeleaf來發送Email

現在在后臺服務器中發送郵件已經是一個非常常用的功能了。通常來說雖然HTML并非是一個非常標準的信息格式,但是至少許多郵件客戶端都至少支持一部分標記語言。 在這邊教程中主要是關于教你如何在Spring Boot 應用中發送郵件以及使用非常簡單強大的Thymeleaf模板引擎來制作郵件內容。

文章末尾附上源碼,已經開源到Github上,是我公司做項目的時候處理郵件這一塊用到的。 基本上覆蓋了大部分郵件發送需求。稍微修改了一下,奉獻給有需要的人。當你看完文章在看一下這封源碼,你會對這一塊更加的了解。而且你能掌握常用的郵件發送:
純文本郵件
內聯圖片郵件
帶附件的郵件

純文本郵件

添加依賴(Mail starter dependencies)

首先制作并且通過SMTP郵件服務器來發送一個純文本郵件。

如果你之前有用過Spring Boot的話,那你寧該并不好奇在你建立一個新工程的時候,Spring Boot已經幫你繼承了常用的依賴庫。 通常你只需要在你的 pom.xml 中添加如下依賴即可:

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

郵件服務器屬性配置(Properties configuration)

通常情況下,如果所需要的依賴在 class path 中都是可用的話,這時候Spring會自動幫你注冊一個默認實現的郵件發送服務 (default mail sender service)。 spring.mail.host 屬性已經被自動定義了, 所有我們所需要做的事情就是把這個屬性添加到我們應用的 application.properties 配置文件中。

application.properties 在resource文件夾下

Spring Boot 提供的默認郵件發送服務 其實已經非常強大了,我們可以通過簡單的配置它的屬性就可以了。所謂的屬性其實說白了就是配置它的郵件SMTP 服務器:

spring.mail.port=25 # SMTP server port
spring.mail.username= # Login used for authentication
spring.mail.password= # Password for the given login
spring.mail.protocol=smtp
spring.mail.defaultEncoding=UTF-8 # Default message encoding

這里附帶一份 gmail 的SMTP服務器配置清單:

spring.mail.host = smtp.gmail.com
spring.mail.username = *****@gmail.com
spring.mail.password = ****
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.socketFactory.port = 587
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false

郵件發送服務(Mail sending service)

在這里我們使用 Autowired 在注入我們的service, 它主要就是生成郵件的相關信息

@Service
public class MailClient {
 
    private JavaMailSender mailSender;
 
    @Autowired
    public MailService(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }
 
    public void prepareAndSend(String recipient, String message) {
        //TODO implement
    }
 
}

生成郵件內容

下面是一個簡單的生成郵件內容的代碼。

public void prepareAndSend(String recipient, String message) {
    MimeMessagePreparator messagePreparator = mimeMessage -> {
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        messageHelper.setFrom("sample@dolszewski.com");
        messageHelper.setTo(recipient);
        messageHelper.setSubject("Sample mail subject");
        messageHelper.setText(message);
    };
    try {
        mailSender.send(messagePreparator);
    } catch (MailException e) {
        // runtime exception; compiler will not force you to handle it
    }
}

send() 需要被重寫以接受不同類型的參數變量:

  • SimpleMailMessage: 正如名字所示,這是一個最基本的郵件message的模塊,我們可以給它設置常用的屬性,它并不能夠修改信息的頭,只能發送純文本的文件。
  • MimeMessage: 通過這個類我們可以構建出比較復雜的郵件內容
  • MimeMessagePreparator: 這是一個接口類,主要目的是提供一個構建模板方法用來構建 MimeMessage 以及當你生成一個實例的時候幫你處理異常信息。官方文檔(也是常識:))建議將MimeMessagePreparator作為郵件構建的首選類型。

MimeMessageHelper類是MimeMessage的裝飾類,它提供了更多的開發人員友好界面,并為類的許多屬性添加了輸入驗證。你可以不用,但是別人肯定會用,而且你會后悔不用 XD。

send() 會拋出 **MailException ** 異常,這是個運行時異常,也就是通常所說的 RuntimeException。 在消息傳遞失敗的情況下,很可能會重復發送操作,或者至少使用一些更復雜的解決方案處理這種情況,例如:使用相應的堆棧跟蹤記錄錯誤消息。

手動測試

通常如果你想郵件功能,你首先需要擁有一個SMTP服務器在你本機的電腦上處理你的請求。 如果你還沒用過,下面給你們推薦一些常用的:

  • FakeSMTP – A simple server written in Java. Supported by any operating system with Java 1.6 or newer installed.
  • smtp4dev – A server with a plain and user friendly interface. For Windows only.
  • Papercut – Another simple server designed for Windows.

集成測試

你可能或許會感到好奇應該如果寫一個自動化的Test來驗證你客戶端的功能。 如果你手動測試的話,你需要開啟SMTP 服務器然后在運行你的Spring Boot客戶端。 在這里給大家推薦一個神器 GreenMail, 因為他跟Junit單元測試高度集成,可以簡化我們的測試。

添加依賴

GreenMail 已經在Maven倉庫中了,所以我們唯一所需要做的就是將其依賴加入我們的 pom.xml 配置文件中:

<dependency>
    <groupId>com.icegreen</groupId>
    <artifactId>greenmail</artifactId>
    <version>1.5.0</version>
    <scope>test</scope>
</dependency>

SMTP服務器與Test模板

現在呢,說了這么多廢話,我們終于可以創建我們的第一個集成測試類了。 它會啟動Spring應用程序并同時運行郵件客戶端。但是在我們編寫實際測試之前呢,我們首先必須要確保SMTP服務器正確運行,同時在測試結束的時候能夠正確關閉。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class MailClientTest {
 
    private GreenMail smtpServer;
 
    @Before
    public void setUp() throws Exception {
        smtpServer = new GreenMail(new ServerSetup(25, null, "smtp"));
        smtpServer.start();
    }
 
    @After
    public void tearDown() throws Exception {
        smtpServer.stop();
    }
     
}

創建郵件客戶端

首先,我們需要注入我們的郵件service在測試類中。之后,我們才能通過GrennMail來驗證是否能夠接受到郵件。

@Autowired
private MailClient mailClient;
 
@Test
public void shouldSendMail() throws Exception {
    //given
    String recipient = "name@hotmail.com";
    String message = "Test message content";
    //when
    mailClient.prepareAndSend(recipient, message);
    //then
    assertReceivedMessageContains(message);
}
 
private void assertReceivedMessageContains(String expected) throws IOException, MessagingException {
    MimeMessage[] receivedMessages = smtpServer.getReceivedMessages();
    assertEquals(1, receivedMessages.length);
    String content = (String) receivedMessages[0].getContent();
    assertTrue(content.contains(expected));
}

發送HTML郵件

在這里我們主要說一下如何構建HTML類型的郵件。

Thymeleaf 模板引擎

首先在你的 pom.xml 中添加依賴。Spring引導將使用其默認設置自動準備引擎

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

Thymeleaf的默認配置期望所有HTML文件都放在 **resources/templates ** 目錄下,以.html擴展名結尾。 讓我們創建一個名為mailTemplate.html的簡單文件,我們將使用創建的郵件客戶端類發送:

11111.PNG

除了在生成過程中作為參數傳遞的消息的占位符,該模板幾乎不包含任何內容。這不是廢話么-,-

模板處理

創建一個服務類,它主要負責將寫入的模板和外部模型組合在一起,這在我們的例子中是一個簡單的短信。

@Service
public class MailContentBuilder {
 
    private TemplateEngine templateEngine;
 
    @Autowired
    public MailContentBuilder(TemplateEngine templateEngine) {
        this.templateEngine = templateEngine;
    }
 
    public String build(String message) {
        Context context = new Context();
        context.setVariable("message", message);
        return templateEngine.process("mailTemplate", context);
    }
 
}

注意這里的 context。 這里主要使用的 鍵值對 的形式,類似map,將模板里面的需要的變量與值對應起來。 比如: <span th:text="${message}"></span>, 這里我們通過context就將message的內容賦值給了span。

TemplateEngine類的實例由Spring Boot Thymeleaf自動配置提供。我們所需要做的就是調用process()方法,該方法接受兩個參數,也就是我們使用的模板的名稱以及充當模型的容器的上下文對象對象。

將新創建的 MailContentBuilder 注入到MailService類中。我們需要在prepareAndSen() 方法中進行一個小的調整,以利用構建器將生成內容設置為mime消息。我們還使用 setText() 方法的重載變量將 Content-Type 頭設置為text / html,而不是默認的 text / plain。

測試

需要更新的最后一件事是我們的測試,更確切地說,是接收到的消息的預期內容。只需對驗證邏輯進行一個小的更改,運行測試并檢查結果。

@Test
public void shouldSendMail() throws Exception {
    //given
    String recipient = "name@dolszewski.com";
    String message = "Test message content";
    //when
    mailService.prepareAndSend(recipient, message);
    //then
    String content = "<span>" + message + "</span>";
    assertReceivedMessageContains(content);
}

本文到此基本宣告結束。
再次獻上一份我常用的html email模板:

        <!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Siemens Sinnovation</title>
    <style>
        .button {
            background-color: #4CAF50;
            border-radius: 12px;
            border: none;
            color: white;
            padding: 10px 25px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 18px;
            margin: 4px 2px;
            cursor: pointer;
            box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
        }

        .button:hover {
            box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);
        }
    </style>
</head>
<body style="margin: 0;padding: 0;">


<table align="center" border="1" cellpadding="0" cellspacing="0" width="600px">
    <tr>
        <td>
            <table align="center" border="0" cellpadding="0" cellspacing="0" width="600"
                   style="border-collapse: collapse;">
                <tr>
                    <td align="center" style="padding: 40px 0 30px 0;">
                        <!--![](image/logo.png)-->
                        ![](|cid:${imageResourceName}|)

                    </td>
                </tr>

                <tr>
                    <td bgcolor="#ffffff" style="padding: 20px 30px 20px 30px">
                        <h4>The following message was created by <span th:text="${owner.getName()}"></span> in the
                            Siemens DFFA group: </h4>


                        <table border="0" cellpadding="0" cellspacing="0" width="100%">
                            <tr>
                                <td><span th:text="${title}">title</span></td>
                            </tr>
                            <tr>
                                <td style="padding: 20px 0 30px 0">
                                    <span th:text="${description}">description</span>

                                </td>

                            </tr>

                            <tr>
                                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                    <tr>
                                        <!--<td align="center" style="padding: 5px 0 3px 0">Status:<span-->
                                                <!--th:text="${status}">status</span></td>-->
                                        <!--<td align="center" style="padding: 5px 0 3px 0">Date submitted: <span-->
                                                <!--th:text="${createDate}">createDate</span></td>-->
                                        <!--<td align="center" style="padding: 5px 0 3px 0">Days left to join:<span-->
                                                <!--th:text="${leftTime}">leftTime</span></td>-->

                                        <td align="center" style="padding: 5px 0 3px 0">Status:<span
                                                th:text="${status}"> OPEN FOR JOINING</span></td>
                                        <td align="center" style="padding: 5px 0 3px 0">Date submitted: 28/08/2017 <span
                                                th:text="${createDate}">createDate</span></td>
                                        <td align="center" style="padding: 5px 0 3px 0">Days left to join: 10h<span
                                                th:text="${leftTime}">leftTime</span></td>
                                    </tr>
                                </table>
                            </tr>
                            <tr>
                                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                    <tr>
                                        <td style="padding: 40px 0px 10px 0px">
                                            Team Member:
                                        </td>
                                    </tr>
                                    <tr th:each="member :${members}">
                                        <td style="padding: 5px 0px 5px 0px"><span
                                                th:text="${member.getName()}+', Email: '+${member.getEmail()}"></span>
                                        </td>
                                    </tr>
                                </table>
                            </tr>
                            <tr>
                                <td align="center" style="padding: 5px 40px 5px 40px">
                                    <button class="button">View Details</button>
                                </td>
                            </tr>
                        </table>


                    </td>
                </tr>
            </table>
        </td>
    </tr>

</table>
</body>
</html>

如果你沒看源碼的話,我敢打賭你看完這篇文章還是一臉蒙蔽 哈哈。
如果喜歡的話,請star吧 謝謝各位看觀老爺
Github 源碼

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容