springboot對JMS提供了很好的支持,對其作了起步依賴。html
構架工程
建立一個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 中填寫本身的郵箱密碼。java
測試發郵件
測試代碼清單以下:git
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](https://my.oschina.net/azibug) public void contextLoads() { } @Autowired private JavaMailSenderImpl mailSender; /** * 發送包含簡單文本的郵件 */ @Test public void sendTxtMail() { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); // 設置收件人,寄件人 simpleMailMessage.setTo(new String[] {"miles02@163.com"}); simpleMailMessage.setFrom("miles02@163.com"); simpleMailMessage.setSubject("Spring Boot Mail 郵件測試【文本】"); simpleMailMessage.setText("這裏是一段簡單文本。"); // 發送郵件 mailSender.send(simpleMailMessage); System.out.println("郵件已發送"); } /** * 發送包含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); // 發送郵件 mailSender.send(mimeMessage); System.out.println("郵件已發送"); } /** * 發送包含內嵌圖片的郵件 * @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指定一個標識 sb.append("<img src=\"cid:imageId\"/></body>"); sb.append("</html>"); // 啓用html mimeMessageHelper.setText(sb.toString(), true); // 設置imageId FileSystemResource img = new FileSystemResource(new File("E:/1.jpg")); mimeMessageHelper.addInline("imageId", img); // 發送郵件 mailSender.send(mimeMessage); System.out.println("郵件已發送"); } /** * 發送包含附件的郵件 * @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); // 設置附件 FileSystemResource img = new FileSystemResource(new File("E:/1.jpg")); mimeMessageHelper.addAttachment("image.jpg", img); // 發送郵件 mailSender.send(mimeMessage); System.out.println("郵件已發送"); } }
測試已所有經過,沒有坑。程序員
參考資料
http://blog.720ui.com/2017/springboot_07_othercore_javamail/github
源碼下載
https://github.com/forezp/SpringBootLearning面試
寫在最後
歡迎關注、喜歡、和點贊後續將推出更多的spring cloud
教程,敬請期待。 歡迎關注個人微信公衆號獲取更多更全的學習資源,視頻資料,技術乾貨! spring
公衆號回覆「學習」,拉你進程序員技術討論羣。小程序
公衆號回覆「視頻」,領取800GJava視頻學習資源。 後端
公衆號回覆「領取資源」,領取1T前端,Java,產品經理,微信小程序,Python等資源合集大放送。
公衆號回覆「慕課」,領取1T慕課實戰學習資源。
公衆號回覆「實戰」,領取750G項目實戰學習資源。
公衆號回覆「面試」,領取8G面試實戰學習資源。
https://blog.csdn.net/forezp/article/details/70341818 本文出自方誌朋的博客