Spring Boot 發(fā)送郵件功能案例分析
郵件服務簡介
郵件服務在互聯網早期就已經出現,如今已成為人們互聯網生活中必不可少的一項服務。那么郵件服務是怎么工作的呢?如下給出郵件發(fā)送與接收的典型過程:
1、發(fā)件人使用SMTP協(xié)議傳輸郵件到郵件服務器A;
2、郵件服務器A根據郵件中指定的接收者,投送郵件至相應的郵件服務器B;
3、收件人使用POP3協(xié)議從郵件服務器B接收郵件。
SMTP(Simple Mail Transfer Protocol)是電子郵件(email)傳輸的互聯網標準,定義在RFC5321,默認使用端口25;
POP3(Post Office Protocol - Version 3)主要用于支持使用客戶端遠程管理在服務器上的電子郵件。定義在RFC 1939,為POP協(xié)議的第三版(最新版)。
這兩個協(xié)議均屬于TCP/IP協(xié)議族的應用層協(xié)議,運行在TCP層之上。
我們日常收發(fā)郵件使用的客戶端、Web Mail的背后都在運行著這兩個協(xié)議,完成收發(fā)郵件的過程。而現在我們需要使用
SMTP協(xié)議來把發(fā)送給用戶的郵件傳輸到郵件服務器。
從客戶端傳輸郵件到服務器需要雙方的配合,而規(guī)則就定義在SMTP協(xié)議中。我們現在需要做的是找一個SMTP服務器,再實現一個SMTP客戶端,然后讓客戶端發(fā)送郵件到服務器。
正文如下
Spring框架使用JavaMailSender接口為發(fā)送郵件提供了一個簡單的抽象,并且Spring Boot也為它提供了自動配置和一個starter模塊。
如果spring.mail.host和相關的庫(通過spring-boot-starter-mail定義)都存在,一個默認的JavaMailSender將被創(chuàng)建。該sender可以通過spring.mail命名空間下的配置項進一步自定義,下面本站素文宅博客具體講述一下Spring Boot如何實現發(fā)送郵件。
引入spring-boot-starter-mail依賴,在pom.xml配置文件中增加如下內容(基于之前章節(jié)“Spring Boot 構建框架”中的pom.xml文件):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
應用發(fā)送郵件案例
在 application.properties 配置文件中加入如下配置(注意替換自己的用戶名和密碼):
spring.mail.host=smtp.qq.com spring.mail.username=用戶名 //發(fā)送方的郵箱 spring.mail.password=密碼 //對于qq郵箱而言 密碼指的就是發(fā)送方的授權碼 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
郵件service服務代碼,具體如下:
@Service public class MailService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender sender; @Value("${spring.mail.username}") private String from; /** * 發(fā)送純文本的簡單郵件 * @param to * @param subject * @param content */ public void sendSimpleMail(String to, String subject, String content){ SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(content); try { sender.send(message); logger.info("簡單郵件已經發(fā)送。"); } catch (Exception e) { logger.error("發(fā)送簡單郵件時發(fā)生異常!", e); } } /** * 發(fā)送html格式的郵件 * @param to * @param subject * @param content */ public void sendHtmlMail(String to, String subject, String content){ MimeMessage message = sender.createMimeMessage(); try { //true表示需要創(chuàng)建一個multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); sender.send(message); logger.info("html郵件已經發(fā)送。"); } catch (MessagingException e) { logger.error("發(fā)送html郵件時發(fā)生異常!", e); } } /** * 發(fā)送帶附件的郵件 * @param to * @param subject * @param content * @param filePath */ public void sendAttachmentsMail(String to, String subject, String content, String filePath){ MimeMessage message = sender.createMimeMessage(); try { //true表示需要創(chuàng)建一個multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); sender.send(message); logger.info("帶附件的郵件已經發(fā)送。"); } catch (MessagingException e) { logger.error("發(fā)送帶附件的郵件時發(fā)生異常!", e); } } /** * 發(fā)送嵌入靜態(tài)資源(一般是圖片)的郵件 * @param to * @param subject * @param content 郵件內容,需要包括一個靜態(tài)資源的id,比如:<img src=\"cid:rscId01\" > * @param rscPath 靜態(tài)資源路徑和文件名 * @param rscId 靜態(tài)資源id */ public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){ MimeMessage message = sender.createMimeMessage(); try { //true表示需要創(chuàng)建一個multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); sender.send(message); logger.info("嵌入靜態(tài)資源的郵件已經發(fā)送。"); } catch (MessagingException e) { logger.error("發(fā)送嵌入靜態(tài)資源的郵件時發(fā)生異常!", e); } } }
簡單測試代碼如下:
public class MailTests extends BasicUtClass{ @Autowired private MailService mailService; private String to = "xujijun@mail.cn"; @Test public void sendSimpleMail() { mailService.sendSimpleMail(to, "主題:簡單郵件", "測試郵件內容"); } }
總結
以上所述是小編給大家介紹的Spring Boot 發(fā)送郵件功能案例分析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
MyBatis學習教程(四)-如何快速解決字段名與實體類屬性名不相同的沖突問題
我們經常會遇到表中的字段名和表對應實體類的屬性名稱不一定都是完全相同的情況,如何解決呢?下面腳本之家小編給大家介紹MyBatis學習教程(四)-如何快速解決字段名與實體類屬性名不相同的沖突問題,一起學習吧2016-05-05SpringBoot中的@Conditional?注解的使用
@Conditional是Spring4新提供的注解,它的作用是按照一定的條件進行判斷,滿足條件的才給容器注冊Bean,本文主要介紹了SpringBoot中的@Conditional?注解的使用2024-01-01Springcloud Config支持本地配置文件的方法示例
這篇文章主要介紹了Springcloud Config支持本地配置文件的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02Java中的interrupt、interrupted和isInterrupted方法區(qū)別詳解
這篇文章主要介紹了Java中的interrupt、interrupted和isInterrupted方法區(qū)別詳解,interrupt用于中斷線程,調用該方法的線程的狀態(tài)將會被設置為中斷狀態(tài),線程中斷僅僅是設置線程的中斷狀態(tài)位,并不會停止線程,需要用戶自己去監(jiān)視線程的狀態(tài)并作出處理,需要的朋友可以參考下2023-12-12