java工具類SendEmailUtil實(shí)現(xiàn)發(fā)送郵件

本文實(shí)例為大家分享了java實(shí)現(xiàn)發(fā)送郵件的工具類,供大家參考,具體內(nèi)容如下

創(chuàng)新互聯(lián)建站主營(yíng)蒙自網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,App定制開發(fā),蒙自h5重慶小程序開發(fā)搭建,蒙自網(wǎng)站營(yíng)銷推廣歡迎蒙自等地區(qū)企業(yè)咨詢

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)碼)
 //在開啟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();
   
  // 開啟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é)議名稱
  props.setProperty("mail.transport.protocol", "smtp");
   
  /**SSL認(rèn)證,注意騰訊郵箱是基于SSL加密的,所以需要開啟才可以使用**/
  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ì)話
  Session session = Session.getInstance(props);
   
  //獲取郵件對(duì)象
  //發(fā)送的消息,基于觀察者模式進(jìn)行設(shè)計(jì)的
  Message msg = new MimeMessage(session);
   
  //設(shè)置郵件標(biāo)題
  msg.setSubject(emailTitle);
   
  //設(shè)置郵件內(nèi)容
  //使用StringBuilder,因?yàn)镾tringBuilder加載速度會(huì)比String快,而且線程安全性也不錯(cuò)
  StringBuilder builder = new StringBuilder();
   
  //寫入內(nèi)容
  builder.append("\n" + emailContent);
   
  //寫入我的官網(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ā)件人郵箱, 顯示的昵稱(只用于顯示, 沒有特別的要求), 昵稱的字符集編碼
  msg.setFrom(new InternetAddress(myEmailAccount,"你好!", "UTF-8"));
   
  //得到郵差對(duì)象
  Transport transport = session.getTransport();
   
  //連接自己的郵箱賬戶
  //密碼不是自己QQ郵箱的密碼,而是在開啟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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站名稱:java工具類SendEmailUtil實(shí)現(xiàn)發(fā)送郵件
網(wǎng)站網(wǎng)址:http://bm7419.com/article26/jjdjcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站收錄、手機(jī)網(wǎng)站建設(shè)定制開發(fā)、App設(shè)計(jì)、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)