使用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)文章
@scope("prototype") @loadbalanced注解負(fù)載均衡失效問題
這篇文章主要為大家介紹了@scope("prototype") @loadbalanced注解負(fù)載均衡失效問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02logback的UNDEFINED_PROPERTY屬性源碼執(zhí)行流程解讀
這篇文章主要為大家介紹了logback的UNDEFINED_PROPERTY屬性源碼執(zhí)行流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11java編程無向圖結(jié)構(gòu)的存儲及DFS操作代碼詳解
這篇文章主要介紹了java編程無向圖結(jié)構(gòu)的存儲及DFS操作代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以了解下。2017-12-12解決SpringCloud?Feign異步調(diào)用傳參問題
這篇文章主要介紹了SpringCloud?Feign異步調(diào)用傳參問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05