SpringBoot3整合郵件服務(wù)實(shí)現(xiàn)郵件發(fā)送功能
內(nèi)容概要
本文介紹了spring boot整合email服務(wù),實(shí)現(xiàn)發(fā)送驗(yàn)證碼,郵件(普通文本郵件、靜態(tài)資源郵件、附件郵件)。
開通服務(wù)
實(shí)現(xiàn)email服務(wù)需要先將自己的郵箱開通POP3/IMAP服務(wù)才可以通過該郵箱發(fā)送郵件,開通步驟如下:
這里以QQ郵箱為例
1.進(jìn)入QQ郵箱官網(wǎng)
2.登錄后點(diǎn)擊設(shè)置

3.選擇賬號(hào)

找到 POP3/IMAP.....服務(wù)這里開啟你的服務(wù),這里我已經(jīng)開啟了的。

4.開啟服務(wù)
開啟成功會(huì)得到授權(quán)碼 ,記住這個(gè)授權(quán)碼。下面開始在spring boot中整合。
spring boot整合
1.依賴引入
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>2.配置屬性
application.yml:
修改成你自己的實(shí)際信息。
spring:
#郵件服務(wù)配置
mail:
host: smtp.qq.com #郵件服務(wù)器地址
protocol: smtp #協(xié)議
username: 305462*****@qq.com #發(fā)送郵件的郵箱也就是你開通服務(wù)的郵箱
password: fiwvcy******d #開通服務(wù)后得到的授權(quán)碼
default-encoding: utf-8 #郵件內(nèi)容的編碼3.創(chuàng)建郵件發(fā)送工具類
使用@component注解標(biāo)記為組件,可以在Service中注入使用。
里面一共定義了三種發(fā)送郵件方法:
- 發(fā)送純文本的普通郵件,可以發(fā)送一些純文本的消息以及驗(yàn)證碼內(nèi)容。
- 發(fā)送HTML格式的文本內(nèi)容,可以發(fā)一些靜態(tài)資源,圖片,音頻,視頻等,也可以發(fā)驗(yàn)證碼信息。
- 發(fā)送攜帶附件的郵件,比如word,excel,pdf文檔或者其他文件。
具體可以根據(jù)業(yè)務(wù)需求調(diào)整代碼。我實(shí)現(xiàn)的較為簡單可以當(dāng)個(gè)模板使用。
import jakarta.annotation.Resource;
import jakarta.mail.internet.MimeMessage;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.Objects;
/**
* @author mijiupro
*/
@Component
public class EmailUtil {
@Resource
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from ;// 發(fā)件人
/**
* 發(fā)送一般郵件--無附件
* @param to 收件人
* @param subject 主題
* @param content 內(nèi)容
* @return 是否成功
*/
@SneakyThrows(Exception.class)
public boolean sendGeneralEmail(String subject, String content, String... to){
// 創(chuàng)建郵件消息
org.springframework.mail.SimpleMailMessage message = new org.springframework.mail.SimpleMailMessage();
message.setFrom(from);
// 設(shè)置收件人
message.setTo(to);
// 設(shè)置郵件主題
message.setSubject(subject);
// 設(shè)置郵件內(nèi)容
message.setText(content);
// 發(fā)送郵件
mailSender.send(message);
return true;
}
/**
* 發(fā)送帶附件的郵件
* @param to 收件人
* @param subject 主題
* @param content 內(nèi)容
* @param filePaths 附件路徑
* @return 是否成功
*/
@SneakyThrows(Exception.class)
public boolean sendAttachmentsEmail(String subject, String content, String[] to, String[] filePaths) {
// 創(chuàng)建郵件消息
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(from);
// 設(shè)置收件人
helper.setTo(to);
// 設(shè)置郵件主題
helper.setSubject(subject);
// 設(shè)置郵件內(nèi)容
helper.setText(content);
// 添加附件
if (filePaths != null) {
for (String filePath : filePaths) {
FileSystemResource file = new FileSystemResource(new File(filePath));
helper.addAttachment(Objects.requireNonNull(file.getFilename()), file);
}
}
// 發(fā)送郵件
mailSender.send(mimeMessage);
return true;
}
/**
* 發(fā)送帶靜態(tài)資源的郵件
* @param to 收件人
* @param subject 主題
* @param content 內(nèi)容
* @param rscPath 靜態(tài)資源路徑
* @param rscId 靜態(tài)資源id
* @return 是否成功
*/
@SneakyThrows(Exception.class)
public boolean sendInlineResourceEmail(String subject, String content, String to, String rscPath, String rscId) {
// 創(chuàng)建郵件消息
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// 設(shè)置發(fā)件人
helper.setFrom(from);
// 設(shè)置收件人
helper.setTo(to);
// 設(shè)置郵件主題
helper.setSubject(subject);
//html內(nèi)容圖片
String contentHtml = "<html><body>這是郵件的內(nèi)容,包含一個(gè)圖片:<img src=\'cid:" + rscId + "\'>"+content+"</body></html>";
helper.setText(contentHtml, true);
//指定講資源地址
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(mimeMessage);
return true;
}
}4.編寫測試用例測試
下面測試前兩種發(fā)送郵件的方法。
測試發(fā)送普通文本郵件
import com.mijiu.commom.util.EmailUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class EmailTest {
@Autowired
private EmailUtil emailUtil;
@Test
public void sendEmail() {
//發(fā)送郵件
boolean b = emailUtil.sendGeneralEmail("測試郵件", " 這是測試郵件", "你的郵箱@163.com");
System.out.println(b);
}
}修改成發(fā)送目標(biāo)郵箱運(yùn)行代碼測試即可。

測試成功。
測試發(fā)送圖片郵件
import com.mijiu.commom.util.EmailUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class EmailTest {
@Autowired
private EmailUtil emailUtil;
@Test
public void sendEmail() {
// 發(fā)送圖片郵件
boolean b = emailUtil.sendInlineResourceEmail("測試郵件", " 這是測試郵件", "你的郵箱@163.com",
"C:\\Users\\mijiupro\\Desktop\\潮.jpg", "C:\\Users\\mijiupro\\Desktop\\潮.jpg");
System.out.println(b);
}
}修改成發(fā)送目標(biāo)郵箱運(yùn)行代碼測試即可。

以上就是SpringBoot3整合郵件服務(wù)實(shí)現(xiàn)郵件發(fā)送功能的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot3郵件發(fā)送的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot+Quartz實(shí)現(xiàn)定時(shí)任務(wù)的代碼模版分享
quartz?是一款開源且豐富特性的Java?任務(wù)調(diào)度庫,用于實(shí)現(xiàn)任務(wù)調(diào)度和定時(shí)任務(wù),本文主要和大家分享一個(gè)SpringBoot整合Quartz實(shí)現(xiàn)定時(shí)任務(wù)的代碼模版,需要的可以參考一下2023-06-06
基于Java實(shí)現(xiàn)本地APK文件搜索與HTTP下載服務(wù)
在移動(dòng)應(yīng)用開發(fā)和測試過程中,經(jīng)常需要從本地服務(wù)器獲取最新的 APK 安裝包,本文將詳細(xì)介紹如何使用 Java 構(gòu)建一個(gè)能夠搜索本地最新 APK 文件并通過 HTTP 請求提供下載服務(wù)的應(yīng)用2025-07-07
java中關(guān)于控件JTextArea的幾個(gè)方法
這篇文章主要介紹了java中關(guān)于控件JTextArea的幾個(gè)方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
java虛擬機(jī)內(nèi)存溢出及泄漏實(shí)例
本篇文章給大家分享了java虛擬機(jī)內(nèi)存溢出及泄漏的實(shí)例以及相關(guān)知識(shí)點(diǎn)分享,有興趣的朋友參考學(xué)習(xí)下。2018-06-06
替換 MyBatis-Plus 默認(rèn)的 saveBatch的實(shí)現(xiàn)步驟
本文主要介紹了替換 MyBatis-Plus 默認(rèn)的 saveBatch的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2026-03-03
SpringBoot參數(shù)校驗(yàn)與國際化使用教程
這篇文章主要給大家介紹了關(guān)于SpringBoot參數(shù)校驗(yàn)與國際化使用教程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

