Spring Boot 發(fā)送郵件功能案例分析
郵件服務(wù)簡(jiǎn)介
郵件服務(wù)在互聯(lián)網(wǎng)早期就已經(jīng)出現(xiàn),如今已成為人們互聯(lián)網(wǎng)生活中必不可少的一項(xiàng)服務(wù)。那么郵件服務(wù)是怎么工作的呢?如下給出郵件發(fā)送與接收的典型過程:
1、發(fā)件人使用SMTP協(xié)議傳輸郵件到郵件服務(wù)器A;
2、郵件服務(wù)器A根據(jù)郵件中指定的接收者,投送郵件至相應(yīng)的郵件服務(wù)器B;
3、收件人使用POP3協(xié)議從郵件服務(wù)器B接收郵件。
SMTP(Simple Mail Transfer Protocol)是電子郵件(email)傳輸?shù)幕ヂ?lián)網(wǎng)標(biāo)準(zhǔn),定義在RFC5321,默認(rèn)使用端口25;
POP3(Post Office Protocol - Version 3)主要用于支持使用客戶端遠(yuǎn)程管理在服務(wù)器上的電子郵件。定義在RFC 1939,為POP協(xié)議的第三版(最新版)。
這兩個(gè)協(xié)議均屬于TCP/IP協(xié)議族的應(yīng)用層協(xié)議,運(yùn)行在TCP層之上。
我們?nèi)粘J瞻l(fā)郵件使用的客戶端、Web Mail的背后都在運(yùn)行著這兩個(gè)協(xié)議,完成收發(fā)郵件的過程。而現(xiàn)在我們需要使用
SMTP協(xié)議來把發(fā)送給用戶的郵件傳輸?shù)洁]件服務(wù)器。
從客戶端傳輸郵件到服務(wù)器需要雙方的配合,而規(guī)則就定義在SMTP協(xié)議中。我們現(xiàn)在需要做的是找一個(gè)SMTP服務(wù)器,再實(shí)現(xiàn)一個(gè)SMTP客戶端,然后讓客戶端發(fā)送郵件到服務(wù)器。
正文如下
Spring框架使用JavaMailSender接口為發(fā)送郵件提供了一個(gè)簡(jiǎn)單的抽象,并且Spring Boot也為它提供了自動(dòng)配置和一個(gè)starter模塊。
如果spring.mail.host和相關(guān)的庫(通過spring-boot-starter-mail定義)都存在,一個(gè)默認(rèn)的JavaMailSender將被創(chuàng)建。該sender可以通過spring.mail命名空間下的配置項(xiàng)進(jìn)一步自定義,下面本站素文宅博客具體講述一下Spring Boot如何實(shí)現(xiàn)發(fā)送郵件。
引入spring-boot-starter-mail依賴,在pom.xml配置文件中增加如下內(nèi)容(基于之前章節(jié)“Spring Boot 構(gòu)建框架”中的pom.xml文件):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
應(yīng)用發(fā)送郵件案例
在 application.properties 配置文件中加入如下配置(注意替換自己的用戶名和密碼):
spring.mail.host=smtp.qq.com spring.mail.username=用戶名 //發(fā)送方的郵箱 spring.mail.password=密碼 //對(duì)于qq郵箱而言 密碼指的就是發(fā)送方的授權(quán)碼 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
郵件service服務(wù)代碼,具體如下:
@Service
public class MailService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private JavaMailSender sender;
@Value("${spring.mail.username}")
private String from;
/**
* 發(fā)送純文本的簡(jiǎn)單郵件
* @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("簡(jiǎn)單郵件已經(jīng)發(fā)送。");
} catch (Exception e) {
logger.error("發(fā)送簡(jiǎn)單郵件時(shí)發(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表示需要?jiǎng)?chuàng)建一個(gè)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郵件已經(jīng)發(fā)送。");
} catch (MessagingException e) {
logger.error("發(fā)送html郵件時(shí)發(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表示需要?jiǎng)?chuàng)建一個(gè)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("帶附件的郵件已經(jīng)發(fā)送。");
} catch (MessagingException e) {
logger.error("發(fā)送帶附件的郵件時(shí)發(fā)生異常!", e);
}
}
/**
* 發(fā)送嵌入靜態(tài)資源(一般是圖片)的郵件
* @param to
* @param subject
* @param content 郵件內(nèi)容,需要包括一個(gè)靜態(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表示需要?jiǎng)?chuàng)建一個(gè)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)資源的郵件已經(jīng)發(fā)送。");
} catch (MessagingException e) {
logger.error("發(fā)送嵌入靜態(tài)資源的郵件時(shí)發(fā)生異常!", e);
}
}
}
簡(jiǎn)單測(cè)試代碼如下:
public class MailTests extends BasicUtClass{
@Autowired
private MailService mailService;
private String to = "xujijun@mail.cn";
@Test
public void sendSimpleMail() {
mailService.sendSimpleMail(to, "主題:簡(jiǎn)單郵件", "測(cè)試郵件內(nèi)容");
}
}
總結(jié)
以上所述是小編給大家介紹的Spring Boot 發(fā)送郵件功能案例分析,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java利用位運(yùn)算實(shí)現(xiàn)加減運(yùn)算詳解
這篇文章主要為大家介紹了如何使用位運(yùn)算來實(shí)現(xiàn)加減功能,也就是在整個(gè)運(yùn)算過程中不能出現(xiàn)加減符號(hào)。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-12-12
Java數(shù)據(jù)結(jié)構(gòu)之紅黑樹的真正理解
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)之紅黑樹的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Springboot中@Transactional注解與異常處理機(jī)制方式
這篇文章主要介紹了Springboot中@Transactional注解與異常處理機(jī)制方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
SpringBoot項(xiàng)目運(yùn)行jar包啟動(dòng)的步驟流程解析
這篇文章主要介紹了SpringBoot項(xiàng)目運(yùn)行jar包啟動(dòng)的步驟流程,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-07-07
SpringBoot整合Shiro實(shí)現(xiàn)權(quán)限控制的代碼實(shí)現(xiàn)
Apache Shiro是一個(gè)強(qiáng)大且易用的Java安全框架,執(zhí)行身份驗(yàn)證、授權(quán)、密碼和會(huì)話管理,今天通過本文給大家介紹SpringBoot整合Shiro實(shí)現(xiàn)權(quán)限控制的方法,感興趣的朋友一起看看吧2021-07-07
Spring?@Conditional通過條件控制bean注冊(cè)過程
這篇文章主要為大家介紹了Spring?@Conditional通過條件控制bean注冊(cè)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
java 記錄一個(gè)子串在整串中出現(xiàn)的次數(shù)實(shí)例
今天小編就為大家分享一篇java 記錄一個(gè)子串在整串中出現(xiàn)的次數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
mybatis-plus多表聯(lián)查join的實(shí)現(xiàn)
本文主要介紹了mybatis-plus多表聯(lián)查join的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

