Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例(附源碼)
快速入門
在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
如其他自動化配置模塊一樣,在完成了依賴引入之后,只需要在application.properties中配置相應的屬性內(nèi)容。
下面我們以QQ郵箱為例,在application.properties中加入如下配置(注意替換自己的用戶名和密碼):
spring.mail.host=smtp.qq.com spring.mail.username=用戶名 spring.mail.password=密碼 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
通過單元測試來實現(xiàn)一封簡單郵件的發(fā)送:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class ApplicationTests {
@Autowired
private JavaMailSender mailSender;
@Test
public void sendSimpleMail() throws Exception {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("dyc87112@qq.com");
message.setTo("dyc87112@qq.com");
message.setSubject("主題:簡單郵件");
message.setText("測試郵件內(nèi)容");
mailSender.send(message);
}
}
到這里,一個簡單的郵件發(fā)送就完成了,運行一下該單元測試,看看效果如何?
“由于Spring Boot的starter模塊提供了自動化配置,所以在引入了spring-boot-starter-mail依賴之后,會根據(jù)配置文件中的內(nèi)容去創(chuàng)建JavaMailSender實例,因此我們可以直接在需要使用的地方直接@Autowired來引入郵件發(fā)送對象。”
進階使用
在上例中,我們通過使用SimpleMailMessage實現(xiàn)了簡單的郵件發(fā)送,但是實際使用過程中,我們還可能會帶上附件、或是使用郵件模塊等。這個時候我們就需要使用MimeMessage來設置復雜一些的郵件內(nèi)容,下面我們就來依次實現(xiàn)一下。
發(fā)送附件
在上面單元測試中加入如下測試用例(通過MimeMessageHelper來發(fā)送一封帶有附件的郵件):
@Test
public void sendAttachmentsMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("dyc87112@qq.com");
helper.setTo("dyc87112@qq.com");
helper.setSubject("主題:有附件");
helper.setText("有附件的郵件");
FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
helper.addAttachment("附件-1.jpg", file);
helper.addAttachment("附件-2.jpg", file);
mailSender.send(mimeMessage);
}
嵌入靜態(tài)資源
除了發(fā)送附件之外,我們在郵件內(nèi)容中可能希望通過嵌入圖片等靜態(tài)資源,讓郵件獲得更好的閱讀體驗,而不是從附件中查看具體圖片,下面的測試用例演示了如何通過MimeMessageHelper實現(xiàn)在郵件正文中嵌入靜態(tài)資源。
@Test
public void sendInlineMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("dyc87112@qq.com");
helper.setTo("dyc87112@qq.com");
helper.setSubject("主題:嵌入靜態(tài)資源");
helper.setText("<html><body><img src=\"cid:weixin\" ></body></html>", true);
FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
helper.addInline("weixin", file);
mailSender.send(mimeMessage);
}
這里需要注意的是addInline函數(shù)中資源名稱weixin需要與正文中cid:weixin對應起來
模板郵件
通常我們使用郵件發(fā)送服務的時候,都會有一些固定的場景,比如重置密碼、注冊確認等,給每個用戶發(fā)送的內(nèi)容可能只有小部分是變化的。所以,很多時候我們會使用模板引擎來為各類郵件設置成模板,這樣我們只需要在發(fā)送時去替換變化部分的參數(shù)即可。
在Spring Boot中使用模板引擎來實現(xiàn)模板化的郵件發(fā)送也是非常容易的,下面我們以velocity為例實現(xiàn)一下。
引入velocity模塊的依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-velocity</artifactId> </dependency>
在resources/templates/下,創(chuàng)建一個模板頁面template.vm:
<html>
<body>
<h3>你好, ${username}, 這是一封模板郵件!</h3>
</body>
</html>
我們之前在Spring Boot中開發(fā)Web應用時,提到過在Spring Boot的自動化配置下,模板默認位于resources/templates/目錄下
最后,我們在單元測試中加入發(fā)送模板郵件的測試用例,具體如下:
@Test
public void sendTemplateMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("dyc87112@qq.com");
helper.setTo("dyc87112@qq.com");
helper.setSubject("主題:模板郵件");
Map<String, Object> model = new HashedMap();
model.put("username", "didi");
String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "template.vm", "UTF-8", model);
helper.setText(text, true);
mailSender.send(mimeMessage);
}
嘗試運行一下,就可以收到內(nèi)容為你好, didi, 這是一封模板郵件!的郵件。這里,我們通過傳入username的參數(shù),在郵件內(nèi)容中替換了模板中的${username}變量。
完整示例:Chapter4-5-1
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
- 深度解析Spring AI請求與響應機制的核心邏輯
- Spring AI實現(xiàn)智能聊天模型
- 深入解析Spring AI框架如何在Java應用中實現(xiàn)智能化交互的關鍵
- Spring AI 入門學習指南
- Spring?AI?+?ollama?本地搭建聊天?AI?功能
- Spring?AI?+?混元帶你實現(xiàn)企業(yè)級穩(wěn)定可部署的AI業(yè)務智能體
- Spring?AI借助全局參數(shù)實現(xiàn)智能數(shù)據(jù)庫操作與個性化待辦管理
- Spring AI 使用超詳細講解
- Spring AI Alibaba 對接百煉平臺大模型使用詳解
- 使用?Spring?AI?+?Ollama?構(gòu)建生成式?AI?應用的方法
- Spring AI源碼分析流式回答(最新推薦)
相關文章
java使用ArrayList實現(xiàn)斗地主(無序版)
這篇文章主要為大家詳細介紹了java使用ArrayList實現(xiàn)斗地主,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03
intellij idea中spring boot properties文件不能自動提示問題解決
這篇文章主要介紹了intellij idea中spring boot properties文件不能自動提示問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02
淺談Arrays.asList() 和ArrayList類型區(qū)別
下面小編就為大家?guī)硪黄狝rrays.asList() 和ArrayList類型區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
springboot+webmagic實現(xiàn)java爬蟲jdbc及mysql的方法
今天小編就為大家分享一篇springboot+webmagic實現(xiàn)java爬蟲jdbc及mysql的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08

