轉(zhuǎn)載請標(biāo)明出處:
http://blog.csdn.net/forezp/article/details/71024024
本文出自方志朋的博客
springboot對JMS提供了很好的支持殉挽,對其做了起步依賴争便。
構(gòu)架工程
創(chuàng)建一個(gè)springboot工程,在其pom文件加入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
添加配置
spring.mail.host=smtp.163.com
spring.mail.username=miles02@163.com
spring.mail.password=
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
在password 中填寫自己的郵箱密碼防楷。
測試發(fā)郵件
測試代碼清單如下:
package com.forezp;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import javax.mail.internet.MimeMessage;
import java.io.File;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootJmsApplicationTests {
@Test
public void contextLoads() {
}
@Autowired
private JavaMailSenderImpl mailSender;
/**
* 發(fā)送包含簡單文本的郵件
*/
@Test
public void sendTxtMail() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
// 設(shè)置收件人,寄件人
simpleMailMessage.setTo(new String[] {"miles02@163.com"});
simpleMailMessage.setFrom("miles02@163.com");
simpleMailMessage.setSubject("Spring Boot Mail 郵件測試【文本】");
simpleMailMessage.setText("這里是一段簡單文本装获。");
// 發(fā)送郵件
mailSender.send(simpleMailMessage);
System.out.println("郵件已發(fā)送");
}
/**
* 發(fā)送包含HTML文本的郵件
* @throws Exception
*/
@Test
public void sendHtmlMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
mimeMessageHelper.setTo("miles02@163.com");
mimeMessageHelper.setFrom("miles02@163.com");
mimeMessageHelper.setSubject("Spring Boot Mail 郵件測試【HTML】");
StringBuilder sb = new StringBuilder();
sb.append("<html><head></head>");
sb.append("<body><h1>spring 郵件測試</h1><p>hello!this is spring mail test老翘。</p></body>");
sb.append("</html>");
// 啟用html
mimeMessageHelper.setText(sb.toString(), true);
// 發(fā)送郵件
mailSender.send(mimeMessage);
System.out.println("郵件已發(fā)送");
}
/**
* 發(fā)送包含內(nèi)嵌圖片的郵件
* @throws Exception
*/
@Test
public void sendAttachedImageMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
// multipart模式
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setTo("miles02@163.com");
mimeMessageHelper.setFrom("miles02@163.com");
mimeMessageHelper.setSubject("Spring Boot Mail 郵件測試【圖片】");
StringBuilder sb = new StringBuilder();
sb.append("<html><head></head>");
sb.append("<body><h1>spring 郵件測試</h1><p>hello!this is spring mail test卢鹦。</p>");
// cid為固定寫法,imageId指定一個(gè)標(biāo)識(shí)
sb.append("<img src=\"cid:imageId\"/></body>");
sb.append("</html>");
// 啟用html
mimeMessageHelper.setText(sb.toString(), true);
// 設(shè)置imageId
FileSystemResource img = new FileSystemResource(new File("E:/1.jpg"));
mimeMessageHelper.addInline("imageId", img);
// 發(fā)送郵件
mailSender.send(mimeMessage);
System.out.println("郵件已發(fā)送");
}
/**
* 發(fā)送包含附件的郵件
* @throws Exception
*/
@Test
public void sendAttendedFileMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
// multipart模式
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
mimeMessageHelper.setTo("miles02@163.com");
mimeMessageHelper.setFrom("miles02@163.com");
mimeMessageHelper.setSubject("Spring Boot Mail 郵件測試【附件】");
StringBuilder sb = new StringBuilder();
sb.append("<html><head></head>");
sb.append("<body><h1>spring 郵件測試</h1><p>hello!this is spring mail test虏冻。</p></body>");
sb.append("</html>");
// 啟用html
mimeMessageHelper.setText(sb.toString(), true);
// 設(shè)置附件
FileSystemResource img = new FileSystemResource(new File("E:/1.jpg"));
mimeMessageHelper.addAttachment("image.jpg", img);
// 發(fā)送郵件
mailSender.send(mimeMessage);
System.out.println("郵件已發(fā)送");
}
}
測試已全部通過肤粱,沒有坑。
參考資料
http://blog.720ui.com/2017/springboot_07_othercore_javamail/
源碼下載
https://github.com/forezp/SpringBootLearning
優(yōu)秀文章推薦:
- 更多springboot 教程:springBoot非官方教程 | 文章匯總
- 更多springcoud 教程:史上最簡單的 SpringCloud 教程 | 文章匯總