Spring Boot使用JavaMailSender發送郵件

Spring提供了很是好用的JavaMailSender接口實現郵件發送。在Spring Boot中也提供了相應的自動化配置。html

這篇文章主要講如何在Spring Boot中使用JavaMailSender發送郵件。java

 

發送郵件web

1,pom.xml中引入spring-boot-starter-mail依賴:spring

 

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-mail</artifactId>

</dependency>

 

 

2,application.properties中配置相應的屬性:(我這裏模擬的是163郵箱給QQ郵箱發送郵件)瀏覽器

spring.mail.host=smtp.163.com spring.mail.username=郵箱用戶名 so****@163.com spring.mail.password=郵箱密碼 spring.mail.default-encoding=UTF-8

 

3,寫發送郵件的測試類mybatis

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/mail") public class MailController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender mailSender; @RequestMapping("/send") public void sendMail(){ SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("so****@163.com"); message.setTo("239****@qq.com"); message.setSubject("it is a test for spring boot"); message.setText("你好,我是小黃,我正在測試發送郵件。"); try { mailSender.send(message); logger.info("小黃的測試郵件已發送。"); } catch (Exception e) { logger.error("小黃髮送郵件時發生異常了!", e); } } }

 

4,運行啓動類app

import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @EnableAutoConfiguration @MapperScan("cn.yideng.*.dao") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } 

 

5,瀏覽器運行 http://localhost:8080/mail/send   ide

6,登陸163郵箱,在發送箱裏查看spring-boot

 

7,登陸QQ郵箱,在收件箱裏查看,以下圖測試

 

 

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

 

固然在實際使用過程當中,不會這麼簡單,咱們可能會要求帶上附件、或使用郵件模塊等。這時咱們就須要使用MimeMessage來設置更復雜的右鍵內容,下面就來看看怎麼實現它。

發送帶附件的郵件

測試類 : 仍是模擬 163郵箱 給QQ郵箱發送郵件

import java.io.File; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 發送有附件的郵件 * */ @RestController @RequestMapping("/mail") public class AttachmentsMailController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender mailSender; @RequestMapping("/att") public void sendMail() throws MessagingException{ MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom("so****@163.com"); helper.setTo("239****@qq.com"); helper.setSubject("主題:發送有附件的郵件"); helper.setText("你好,我是小黃,我正在測試發送一封有附件的郵件。"); FileSystemResource file1 = new FileSystemResource(new File("d:\\cat.jpg")); FileSystemResource file2 = new FileSystemResource(new File("d:\\java-1.jpg")); helper.addAttachment("附件-1.jpg", file1); helper.addAttachment("附件-2.jpg", file2); try { mailSender.send(mimeMessage); logger.info("小黃的測試帶附件的郵件已發送。"); } catch (Exception e) { logger.error("小黃髮送帶附件郵件時發生異常了!", e); } } }

2,須要 D盤下準備兩張圖片cat.jpg  java-1.jpg,

 

3,瀏覽器運行 http://localhost:8080/mail/att   

4,登陸163郵箱,在發送箱裏查看,以下圖

 

5,登陸QQ郵箱,在收件箱裏查看,以下圖

 

 

嵌入靜態資源的郵件

還有一種是經過嵌入圖片等靜態資源,能夠直接看到圖片,而不用從附近中查看具體的圖片,來看看吧。

測試類:

import java.io.File; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 嵌入靜態資源 */ @RestController @RequestMapping("/mail") public class StaticResourceMailController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender mailSender; @RequestMapping("/static") public void sendMail() throws MessagingException{ MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom("so****@163.com"); helper.setTo("239****@qq.com"); helper.setSubject("主題:嵌入靜態資源"); helper.setText("<html><body><img src=\"cid:hello\" ></body></html>", true); // 注意addInline()中資源名稱 hello 必須與 text正文中cid:hello對應起來
 FileSystemResource file1 = new FileSystemResource(new File("d:\\cat.jpg")); helper.addInline("hello", file1); try { mailSender.send(mimeMessage); logger.info("小黃的測試嵌入靜態資源的郵件已發送。"); } catch (Exception e) { logger.error("小黃髮送嵌入靜態資源的郵件時發生異常了!", e); } } }

 

要特別注意addInline()中資源名稱 hello 必須與 text正文中cid:hello對應

2,須要 在D盤下準備兩張圖片cat.jpg

3,瀏覽器運行 http://localhost:8080/mail/static

4,登陸163郵箱,在發送箱裏查看,以下圖

 

5,登陸QQ郵箱,在收件箱裏查看,以下圖

 

 

好了,Spring Boot使用JavaMailSender發送郵件就到這裏了。

相關文章
相關標籤/搜索