快速入門
在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依賴:
1
2
3
4
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-mail</ artifactId > </ dependency > |
如其他自動化配置模塊一樣,在完成了依賴引入之后,只需要在application.properties
中配置相應的屬性內容。
下面我們以QQ郵箱為例,在application.properties
中加入如下配置(注意替換自己的用戶名和密碼):
1
2
3
4
5
6
|
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 |
通過單元測試來實現一封簡單郵件的發送:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@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.setSubject( "主題:簡單郵件" ); message.setText( "測試郵件內容" ); mailSender.send(message); } } |
到這里,一個簡單的郵件發送就完成了,運行一下該單元測試,看看效果如何?
“由于Spring Boot的starter模塊提供了自動化配置,所以在引入了spring-boot-starter-mail依賴之后,會根據配置文件中的內容去創建JavaMailSender實例,因此我們可以直接在需要使用的地方直接@Autowired來引入郵件發送對象。”
進階使用
在上例中,我們通過使用SimpleMailMessage實現了簡單的郵件發送,但是實際使用過程中,我們還可能會帶上附件、或是使用郵件模塊等。這個時候我們就需要使用MimeMessage來設置復雜一些的郵件內容,下面我們就來依次實現一下。
發送附件
在上面單元測試中加入如下測試用例(通過MimeMessageHelper來發送一封帶有附件的郵件):
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Test public void sendAttachmentsMail() throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true ); 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); } |
嵌入靜態資源
除了發送附件之外,我們在郵件內容中可能希望通過嵌入圖片等靜態資源,讓郵件獲得更好的閱讀體驗,而不是從附件中查看具體圖片,下面的測試用例演示了如何通過MimeMessageHelper實現在郵件正文中嵌入靜態資源。
1
2
3
4
5
6
7
8
9
10
11
12
|
@Test public void sendInlineMail() throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true ); helper.setSubject( "主題:嵌入靜態資源" ); 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函數中資源名稱weixin需要與正文中cid:weixin對應起來
模板郵件
通常我們使用郵件發送服務的時候,都會有一些固定的場景,比如重置密碼、注冊確認等,給每個用戶發送的內容可能只有小部分是變化的。所以,很多時候我們會使用模板引擎來為各類郵件設置成模板,這樣我們只需要在發送時去替換變化部分的參數即可。
在Spring Boot中使用模板引擎來實現模板化的郵件發送也是非常容易的,下面我們以velocity為例實現一下。
引入velocity模塊的依賴:
1
2
3
4
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-velocity</ artifactId > </ dependency > |
在resources/templates/下,創建一個模板頁面template.vm:
1
2
3
4
5
|
< html > < body > < h3 >你好, ${username}, 這是一封模板郵件!</ h3 > </ body > </ html > |
我們之前在Spring Boot中開發Web應用時,提到過在Spring Boot的自動化配置下,模板默認位于resources/templates/目錄下
最后,我們在單元測試中加入發送模板郵件的測試用例,具體如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Test public void sendTemplateMail() throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true ); 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); } |
嘗試運行一下,就可以收到內容為你好, didi, 這是一封模板郵件!的郵件。這里,我們通過傳入username的參數,在郵件內容中替換了模板中的${username}
變量。
完整示例:Chapter4-5-1
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
原文鏈接:http://blog.didispace.com/springbootmailsender/