Spring Boot中使用JavaMailSender發送郵件

原文:http://www.cnblogs.com/yucongblog/p/7385434.htmlhtml

 

 

相信使用過Spring的衆多開發者都知道Spring提供了很是好用的JavaMailSender接口實現郵件發送。在Spring Boot的Starter模塊中也爲此提供了自動化配置。下面經過實例看看如何在Spring Boot中使用JavaMailSender發送郵件。spring

快速入門

在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依賴:app

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

如其餘自動化配置模塊同樣,在完成了依賴引入以後,只須要在application.properties中配置相應的屬性內容。函數

下面咱們以QQ郵箱爲例,在application.properties中加入以下配置(注意替換本身的用戶名和密碼):spring-boot

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
16
17
18
19
@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( "測試郵件內容");
 
mailSender.send(message);
}
 
}

到這裏,一個簡單的郵件發送就完成了,運行一下該單元測試,看看效果如何?測試

因爲Spring Boot的starter模塊提供了自動化配置,因此在引入了spring-boot-starter-mail依賴以後,會根據配置文件中的內容去建立JavaMailSender實例,所以咱們能夠直接在須要使用的地方直接@Autowired來引入郵件發送對象。ui

進階使用

在上例中,咱們經過使用SimpleMailMessage實現了簡單的郵件發送,可是實際使用過程當中,咱們還可能會帶上附件、或是使用郵件模塊等。這個時候咱們就須要使用MimeMessage來設置複雜一些的郵件內容,下面咱們就來依次實現一下。spa

發送附件

在上面單元測試中加入以下測試用例(經過MimeMessageHelper來發送一封帶有附件的郵件):code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@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);
 
}

嵌入靜態資源

除了發送附件以外,咱們在郵件內容中可能但願經過嵌入圖片等靜態資源,讓郵件得到更好的閱讀體驗,而不是從附件中查看具體圖片,下面的測試用例演示瞭如何經過MimeMessageHelper實如今郵件正文中嵌入靜態資源。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@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( "主題:嵌入靜態資源");
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
15
16
17
18
@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);
}

嘗試運行一下,就能夠收到內容爲你好, didi, 這是一封模板郵件!的郵件。這裏,咱們經過傳入username的參數,在郵件內容中替換了模板中的${username}變量。

相關文章
相關標籤/搜索