JavaMail實(shí)現(xiàn)發(fā)送郵件(QQ郵箱)
本文實(shí)例為大家分享了JavaMail實(shí)現(xiàn)發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下
用的qq郵箱,需要去郵箱設(shè)置那邊開(kāi)一下stmp服務(wù)啥的獲得下面要用到的密碼,具體開(kāi)服務(wù)自己百度,這邊不截圖了。
代碼如下:導(dǎo)包和工具類(lèi),可用!
一、導(dǎo)這個(gè)包
<dependency> ? ? <groupId>javax.mail</groupId> ? ? ?<artifactId>mail</artifactId> ? ? <version>1.5.0-b01</version> </dependency>
二、工具類(lèi)封裝成對(duì)象
import lombok.Data; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class MailHelper { ? ? /** ? ? ?* 郵件服務(wù)器主機(jī)名。 ? ? ?*/ ? ? private static String HOST_NAME; ? ? private String sendMailUrl ; ? ? private String receiveMailUrl; ? ? /** ? ? ?* IMAP/SMTP服務(wù)的密碼 去qq郵箱開(kāi)的。 幾個(gè)月前的失效了還是蠻奇怪的 2021年8月5日21:33:36 ? ? ?*/ ? ? private String servicePassword; ? ? static { ? ? ? ? //默認(rèn)直接qq的吧 ? ? ? ? HOST_NAME = "smtp.qq.com"; ? ? } ? ? public MailHelper(String sendMailUrl,String receiveMailUrl,String servicePassword){ ? ? ? ? this.sendMailUrl=sendMailUrl; ? ? ? ? this.receiveMailUrl=receiveMailUrl; ? ? ? ? this.servicePassword=servicePassword; ? ? } ? ? public ?void sendSimpleMail(Mail mail) throws Exception { ? ? ? ? Properties prop = new Properties(); ? ? ? ? ?設(shè)置郵件服務(wù)器主機(jī)名 ? ? ? ? prop.setProperty("mail.host", "smtp.qq.com"); ? ? ? ? // 發(fā)送郵件協(xié)議名稱(chēng) ? ? ? ? prop.setProperty("mail.transport.protocol", "smtp"); ? ? ? ? prop.setProperty("mail.smtp.auth", "true"); ? ? ? ? // 使用JavaMail發(fā)送郵件的5個(gè)步驟 ? ? ? ? // 1、創(chuàng)建session 根據(jù)配置創(chuàng)建會(huì)話(huà)對(duì)象, 用于和郵件服務(wù)器交互 ? ? ? ? Session session = Session.getInstance(prop); ? ? ? ? // 開(kāi)啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運(yùn)行狀態(tài) ? ? ? ? session.setDebug(true); ? ? ? ? // 2、通過(guò)session得到transport對(duì)象 ? ? ? ? Transport ts = session.getTransport(); ? ? ? ? // 3、使用郵箱的用戶(hù)名和密碼連上郵件服務(wù)器,發(fā)送郵件時(shí),發(fā)件人需要提交郵箱的用戶(hù)名和密碼給smtp服務(wù)器,用戶(hù)名和密碼都通過(guò)驗(yàn)證之后才能夠正常發(fā)送郵件給收件人。 ? ? ? ? // 注:這邊host必須填寫(xiě)smtp.qq.com ? ? ? ? // 而不是你qq郵箱賬號(hào)如1741049@qq.com,否則報(bào)錯(cuò)host名unkonwn。 ? ? ? ? // Host:郵件服務(wù)器主機(jī)名 ? ? ? ? ts.connect(HOST_NAME, receiveMailUrl, servicePassword); ? ? ? ? // 4、創(chuàng)建郵件 ? ? ? ? Message message = createSimpleMail(session, mail.getTitle(), mail.getContent()); ? ? ? ? // 5、發(fā)送郵件 ? ? ? ? ts.sendMessage(message, message.getAllRecipients()); ? ? ? ? ts.close(); ? ? } ? ?? ? ? private MimeMessage createSimpleMail(Session session, String title, String content) throws Exception { ? ? ? ? // 創(chuàng)建郵件對(duì)象 ? ? ? ? MimeMessage message = new MimeMessage(session); ? ? ? ? // 指明郵件的發(fā)件人 ? ? ? ? message.setFrom(new InternetAddress(receiveMailUrl)); ? ? ? ? // 指明郵件的收件人,現(xiàn)在發(fā)件人和收件人是一樣的,那就是自己給自己發(fā) ? ? ? ? message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiveMailUrl)); ? ? ? ? // 郵件的標(biāo)題 ? ? ? ? message.setSubject(title); ? ? ? ? // 郵件的文本內(nèi)容 ? ? ? ? message.setContent(content, "text/html;charset=UTF-8"); ? ? ? ? // 返回創(chuàng)建好的郵件對(duì)象 ? ? ? ? return message; ? ? } ? ? @Data ? ? public static class Mail{ ? ? ? ? private String title; ? ? ? ? /** ? ? ? ? ?* 正文 ? ? ? ? ?*/ ? ? ? ? private String content; ? ? ? ? public Mail(){ ? ? ? ? } ? ? ? ? public Mail(String title,String content){ ? ? ? ? ? ? this.title=title; ? ? ? ? ? ? this.content=content; ? ? ? ? } ? ? } }
測(cè)試類(lèi):
package com.forever.junittest; import com.forever.gitfund.util.MailHelper; import com.forever.gitfund.util.MailHelper.Mail; import org.junit.Test; public class TestMailHelper { ? ? @Test ? ? public void test() throws Exception { ? ? ? ? String qq = "xx@qq.com"; ? ? ? ? String send = qq; ? ? ? ? MailHelper mailHelper = new MailHelper(qq,send,"xxxxx"); ? ? ? ? Mail mail = new Mail(); ? ? ? ? String title = "我的第一封郵件"; ? ? ? ? String content = "這是我的第一封郵件 from idea"; ? ? ? ? mail.setTitle(title); ? ? ? ? mail.setContent(content); ? ? ? ? mailHelper.sendSimpleMail(mail); ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java隨手筆記8之包、環(huán)境變量和訪(fǎng)問(wèn)控制及maven profile實(shí)現(xiàn)多環(huán)境打包
這篇文章主要介紹了Java隨手筆記8之包、環(huán)境變量和訪(fǎng)問(wèn)控制及maven profile實(shí)現(xiàn)多環(huán)境打包的相關(guān)資料,需要的朋友可以參考下2015-11-11Java構(gòu)造函數(shù)里的一些坑記錄super()和this()
這篇文章主要介紹了Java構(gòu)造函數(shù)里的一些坑記錄super()和this(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Springboot 實(shí)現(xiàn)跨域訪(fǎng)問(wèn)無(wú)需使用jsonp的實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot 實(shí)現(xiàn)跨域訪(fǎng)問(wèn) 無(wú)需使用jsonp的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09SpringBoot實(shí)現(xiàn)WEB的常用功能案例詳解
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)WEB的常用功能,本文將對(duì)Spring Boot實(shí)現(xiàn)Web開(kāi)發(fā)中涉及的三大組件Servlet、Filter、Listener以及文件上傳下載功能以及打包部署進(jìn)行實(shí)現(xiàn),需要的朋友可以參考下2022-04-04java static塊和構(gòu)造函數(shù)的實(shí)例詳解
這篇文章主要介紹了java static塊和構(gòu)造函數(shù)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解掌握J(rèn)ava static關(guān)鍵字的函數(shù)方法,需要的朋友可以參考下2017-09-09jsp+dao+bean+servlet(MVC模式)實(shí)現(xiàn)簡(jiǎn)單用戶(hù)登錄和注冊(cè)頁(yè)面
這篇文章主要介紹了jsp+dao+bean+servlet(MVC模式)實(shí)現(xiàn)簡(jiǎn)單用戶(hù)登錄和注冊(cè)頁(yè)面,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Mybatis-plus在項(xiàng)目中的簡(jiǎn)單應(yīng)用
Mybatis-plus是Spring框架中OOM的一大利器,其簡(jiǎn)單易用參考官網(wǎng)文檔即可很快上手,本文主要介紹了邏輯刪除,自動(dòng)填充,分頁(yè)插件等的簡(jiǎn)單使用,感興趣的可以了解一下2021-07-07基于SpringBoot應(yīng)用監(jiān)控Actuator安全隱患及解決方式
這篇文章主要介紹了SpringBoot應(yīng)用監(jiān)控Actuator安全隱患及解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07