java工具類(lèi)SendEmailUtil實(shí)現(xiàn)發(fā)送郵件
本文實(shí)例為大家分享了java實(shí)現(xiàn)發(fā)送郵件的工具類(lèi),供大家參考,具體內(nèi)容如下
SendEmailUtil
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.5</version> </dependency>
import javax.mail.Address; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.sun.mail.util.MailSSLSocketFactory; import java.io.FileOutputStream; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; public class SendmailUtil { //郵件服務(wù)器主機(jī)名 // QQ郵箱的 SMTP 服務(wù)器地址為: smtp.qq.com private static String myEmailSMTPHost = "smtp.qq.com"; //發(fā)件人郵箱 private static String myEmailAccount = "xxxxxxxxxx@xx.com"; //發(fā)件人郵箱密碼(授權(quán)碼) //在開(kāi)啟SMTP服務(wù)時(shí)會(huì)獲取到一個(gè)授權(quán)碼,把授權(quán)碼填在這里 private static String myEmailPassword = "xxxxxxxxxxxx"; /** * 郵件單發(fā)(自由編輯短信,并發(fā)送,適用于私信) * * @param toEmailAddress 收件箱地址 * @param emailTitle 郵件主題 * @param emailContent 郵件內(nèi)容 * @throws Exception */ public static void sendEmail(String toEmailAddress, String emailTitle, String emailContent) throws Exception{ Properties props = new Properties(); // 開(kāi)啟debug調(diào)試 props.setProperty("mail.debug", "true"); // 發(fā)送服務(wù)器需要身份驗(yàn)證 props.setProperty("mail.smtp.auth", "true"); // 端口號(hào) props.put("mail.smtp.port", 465); // 設(shè)置郵件服務(wù)器主機(jī)名 props.setProperty("mail.smtp.host", myEmailSMTPHost); // 發(fā)送郵件協(xié)議名稱(chēng) props.setProperty("mail.transport.protocol", "smtp"); /**SSL認(rèn)證,注意騰訊郵箱是基于SSL加密的,所以需要開(kāi)啟才可以使用**/ MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); //設(shè)置是否使用ssl安全連接(一般都使用) props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); //創(chuàng)建會(huì)話(huà) Session session = Session.getInstance(props); //獲取郵件對(duì)象 //發(fā)送的消息,基于觀(guān)察者模式進(jìn)行設(shè)計(jì)的 Message msg = new MimeMessage(session); //設(shè)置郵件標(biāo)題 msg.setSubject(emailTitle); //設(shè)置郵件內(nèi)容 //使用StringBuilder,因?yàn)镾tringBuilder加載速度會(huì)比String快,而且線(xiàn)程安全性也不錯(cuò) StringBuilder builder = new StringBuilder(); //寫(xiě)入內(nèi)容 builder.append("\n" + emailContent); //寫(xiě)入我的官網(wǎng) builder.append("\n官網(wǎng):" + "https://www.hbuecx.club"); //定義要輸出日期字符串的格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //在內(nèi)容后加入郵件發(fā)送的時(shí)間 builder.append("\n時(shí)間:" + sdf.format(new Date())); //設(shè)置顯示的發(fā)件時(shí)間 msg.setSentDate(new Date()); //設(shè)置郵件內(nèi)容 msg.setText(builder.toString()); //設(shè)置發(fā)件人郵箱 // InternetAddress 的三個(gè)參數(shù)分別為: 發(fā)件人郵箱, 顯示的昵稱(chēng)(只用于顯示, 沒(méi)有特別的要求), 昵稱(chēng)的字符集編碼 msg.setFrom(new InternetAddress(myEmailAccount,"你好!", "UTF-8")); //得到郵差對(duì)象 Transport transport = session.getTransport(); //連接自己的郵箱賬戶(hù) //密碼不是自己QQ郵箱的密碼,而是在開(kāi)啟SMTP服務(wù)時(shí)所獲取到的授權(quán)碼 //connect(host, user, password) transport.connect( myEmailSMTPHost, myEmailAccount, myEmailPassword); //發(fā)送郵件 transport.sendMessage(msg, new Address[] { new InternetAddress(toEmailAddress) }); //將該郵件保存到本地 OutputStream out = new FileOutputStream("MyEmail.eml"); msg.writeTo(out); out.flush(); out.close(); transport.close(); } }
//toEmailAddress 目標(biāo)郵箱地址 //emailTitle 郵件標(biāo)題 //emailContent 郵件內(nèi)容 SendmailUtil.sendEmail(toEmailAddress, emailTitle, emailContent);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java Builder模式構(gòu)建MAP/LIST的實(shí)例講解
下面小編就為大家?guī)?lái)一篇Java Builder模式構(gòu)建MAP/LIST的實(shí)例講解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Mybatis Plus使用條件構(gòu)造器增刪改查功能的實(shí)現(xiàn)方法
這篇文章主要介紹了Mybatis-Plus使用條件構(gòu)造器增刪改查,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05關(guān)于springboot忽略接口,參數(shù)注解的使用ApiIgnore
這篇文章主要介紹了關(guān)于springboot忽略接口,參數(shù)注解的使用ApiIgnore,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性
這篇文章主要介紹了關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性,需要的朋友可以參考下2023-07-07AbstractQueuedSynchronizer內(nèi)部類(lèi)Node使用講解
這篇文章主要為大家介紹了AbstractQueuedSynchronizer內(nèi)部類(lèi)Node使用講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07springboot后臺(tái)session的存儲(chǔ)與取出方式
這篇文章主要介紹了springboot后臺(tái)session的存儲(chǔ)與取出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06