使用JAVA實(shí)現(xiàn)郵件發(fā)送功能的圖文教程
一、準(zhǔn)備工作
小編今天以 QQ郵箱 進(jìn)行演示操作。
想要使用代碼操作郵箱發(fā)送郵件,需要在郵箱設(shè)置中申請開通 POP3/SMTP 服務(wù)。
接下來跟著小編的圖文一步一步的操作開通吧!
1.1 登錄網(wǎng)頁QQ郵箱,點(diǎn)擊頁面頂部設(shè)置按鈕。

1.2 點(diǎn)擊后會打開郵箱設(shè)置頁面,如下所示,點(diǎn)擊第二欄賬戶。

1.3 點(diǎn)擊后往下拉,直到有如下頁面選項(xiàng)。

選擇POP3/SMTP服務(wù),點(diǎn)擊后面的開啟,此時(shí)會讓你使用綁定郵箱的手機(jī)號發(fā)送短信。

發(fā)送完成后點(diǎn)擊 我已發(fā)送 按鈕,進(jìn)行驗(yàn)證。
1.4 驗(yàn)證完成后會顯示授權(quán)碼(復(fù)制下來,一會要用)

到此,準(zhǔn)備工作已完成!
二、項(xiàng)目中配置郵件發(fā)送功能
2.1 引入發(fā)送郵件相關(guān)依賴
<!--郵件發(fā)送依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
引入后,更新maven,下載相關(guān)依賴

2.2 在application.yml文件中添加郵件基本配置
spring:
# 發(fā)送郵件配置
mail:
host: smtp.qq.com # 配置 smtp 服務(wù)器地址
port: 587 # smtp 服務(wù)器的端口
username: 1354720889@qq.com # 配置郵箱用戶名(你的郵箱地址)
password: batrgddaqykegfss # 配置申請到的授權(quán)碼(剛讓復(fù)制的授權(quán)碼)
default-encoding: UTF-8 # 配置郵件編碼
properties:
mail:
smtp:
socketFactoryClass: javax.net.ssl.SSLSocketFactory # 配飾 SSL 加密工廠
debug: true
from: 1354720889@qq.com # 發(fā)送方郵件,陪在yml中可方便更改
2.3 為了方便使用,新建一個(gè)操作email的工具類(EmailUtil.java)
封裝郵件工具類是為了方便后續(xù)調(diào)用操作。
package com.clover.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* @ClassName EmailUtil
* @Description 郵件發(fā)送工具
* @Author Sophia
* @Date 2022/4/6 16:06
*/
@Component
public class EmailUtil {
@Value("${spring.mail.from}") // 從application.yml配置文件中獲取
private String from; // 發(fā)送發(fā)郵箱地址
@Autowired
private JavaMailSender mailSender;
/**
* 發(fā)送純文本郵件信息
*
* @param to 接收方
* @param subject 郵件主題
* @param content 郵件內(nèi)容(發(fā)送內(nèi)容)
*/
public void sendMessage(String to, String subject, String content) {
// 創(chuàng)建一個(gè)郵件對象
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom(from); // 設(shè)置發(fā)送發(fā)
msg.setTo(to); // 設(shè)置接收方
msg.setSubject(subject); // 設(shè)置郵件主題
msg.setText(content); // 設(shè)置郵件內(nèi)容
// 發(fā)送郵件
mailSender.send(msg);
}
/**
* 發(fā)送帶附件的郵件信息
*
* @param to 接收方
* @param subject 郵件主題
* @param content 郵件內(nèi)容(發(fā)送內(nèi)容)
* @param files 文件數(shù)組 // 可發(fā)送多個(gè)附件
*/
public void sendMessageCarryFiles(String to, String subject, String content, File[] files) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(from); // 設(shè)置發(fā)送發(fā)
helper.setTo(to); // 設(shè)置接收方
helper.setSubject(subject); // 設(shè)置郵件主題
helper.setText(content); // 設(shè)置郵件內(nèi)容
if (files != null && files.length > 0) { // 添加附件(多個(gè))
for (File file : files) {
helper.addAttachment(file.getName(), file);
}
}
} catch (MessagingException e) {
e.printStackTrace();
}
// 發(fā)送郵件
mailSender.send(mimeMessage);
}
/**
* 發(fā)送帶附件的郵件信息
*
* @param to 接收方
* @param subject 郵件主題
* @param content 郵件內(nèi)容(發(fā)送內(nèi)容)
* @param file 單個(gè)文件
*/
public void sendMessageCarryFile(String to, String subject, String content, File file) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(from); // 設(shè)置發(fā)送發(fā)
helper.setTo(to); // 設(shè)置接收方
helper.setSubject(subject); // 設(shè)置郵件主題
helper.setText(content); // 設(shè)置郵件內(nèi)容
helper.addAttachment(file.getName(), file); // 單個(gè)附件
} catch (MessagingException e) {
e.printStackTrace();
}
// 發(fā)送郵件
mailSender.send(mimeMessage);
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
}2.4 測試郵件發(fā)送功能
我使用的SpringBoot項(xiàng)目,因此我在測試類中進(jìn)行測試。
測試類代碼:
package com.clover.api.blogapi;
import com.clover.utils.EmailUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
@SpringBootTest
class BlogApiApplicationTests {
@Autowired
private EmailUtil emailUtil;
@Test
void contextLoads() {
}
@Test
void sendStringEmail() {
// 測試文本郵件發(fā)送(無附件)
String to = "1354720990@qq.com";
String title = "文本郵件發(fā)送測試";
String content = "文本郵件發(fā)送測試";
emailUtil.sendMessage(to, title, content);
}
@Test
void sendFileEmail() {
// 測試單個(gè)附件郵件發(fā)送
String to = "1354720990@qq.com";
String title = "單個(gè)附件郵件發(fā)送測試";
String content = "單個(gè)附件郵件發(fā)送測試";
File file = new File("D:\\GCH\\Typora\\Linux中常用的查看系統(tǒng)相關(guān)信息命令.md");
emailUtil.sendMessageCarryFile(to, title, content, file);
}
@Test
void sendFilesEmail() {
// 測試多個(gè)附件郵件發(fā)送
String to = "1354720990@qq.com";
String title = "多個(gè)附件郵件發(fā)送測試";
String content = "多個(gè)附件郵件發(fā)送測試";
File[] files = new File[2];
files[0] = new File("C:\\Users\\root\\Desktop\\配置郵箱\\1.png");
files[1] = new File("C:\\Users\\root\\Desktop\\配置郵箱\\2.png");
emailUtil.sendMessageCarryFile(to, title, content, files);
}
}2.5 測試結(jié)果
郵箱已收到

2.5.1 文本郵件發(fā)送

2.5.2 單個(gè)附件郵件發(fā)送

2.5.3 多個(gè)附件郵件發(fā)送

到此,使用Java發(fā)送郵件功能就結(jié)束了,伙伴們可以根據(jù)自己的需求進(jìn)行封裝使用。
總結(jié)
到此這篇關(guān)于使用JAVA實(shí)現(xiàn)郵件發(fā)送功能的文章就介紹到這了,更多相關(guān)JAVA郵件發(fā)送功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java中javamail發(fā)送帶附件的郵件實(shí)現(xiàn)方法
- Java郵件發(fā)送程序(可以同時(shí)發(fā)給多個(gè)地址、可以帶附件)
- java編程實(shí)現(xiàn)郵件定時(shí)發(fā)送的方法
- java 發(fā)送郵件的實(shí)例代碼(可移植)
- Java實(shí)現(xiàn)帶附件的郵件發(fā)送功能
- 使用Java實(shí)現(xiàn)qq郵箱發(fā)送郵件
- JavaWeb實(shí)現(xiàn)郵件發(fā)送接收功能
- java發(fā)送郵件示例講解
- Java實(shí)現(xiàn)郵件發(fā)送功能
- java發(fā)送郵件的具體實(shí)現(xiàn)
相關(guān)文章
SSM框架中測試單元的使用 spring整合Junit過程詳解
這篇文章主要介紹了SSM框架中測試單元的使用 spring整合Junit過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
java編程經(jīng)典案例之基于斐波那契數(shù)列解決兔子問題實(shí)例
這篇文章主要介紹了java編程經(jīng)典案例之基于斐波那契數(shù)列解決兔子問題,結(jié)合完整實(shí)例形式分析了斐波那契數(shù)列的原理及java解決兔子問題的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
基于maven使用IDEA創(chuàng)建多模塊項(xiàng)目
這篇文章主要介紹了基于maven使用IDEA創(chuàng)建多模塊項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
SpringBoot如何優(yōu)雅的處理重復(fù)請求
對于一些用戶請求,在某些情況下是可能重復(fù)發(fā)送的,如果是查詢類操作并無大礙,但其中有些是涉及寫入操作的,一旦重復(fù)了,可能會導(dǎo)致很嚴(yán)重的后果,所以本文給大家介紹了SpringBoot優(yōu)雅的處理重復(fù)請求的方法,需要的朋友可以參考下2023-12-12
SpringBoot啟動嵌入式Tomcat的實(shí)現(xiàn)步驟
本文主要介紹了淺談SpringBoot如何啟動嵌入式Tomcat,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Java使用Junit4.jar進(jìn)行單元測試的方法
今天通過本文給大家介紹Java使用Junit4.jar進(jìn)行單元測試的方法,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-11-11

