欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)163郵箱發(fā)送郵件到qq郵箱成功案例

 更新時(shí)間:2016年05月19日 21:52:48   作者:Past_Future  
這篇文章主要為大家分享了java實(shí)現(xiàn)163郵箱發(fā)送郵件到qq郵箱成功案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

下載和上傳附件、發(fā)送短信和發(fā)送郵件,都算是程序中很常用的功能,之前記錄了文件的上傳和下載還有發(fā)送短信,由于最近比較忙,郵件發(fā)送的功能就沒有時(shí)間去弄,現(xiàn)在終于成功以163郵箱發(fā)送郵件到qq郵箱,以下是相關(guān)代碼,具體解釋可以參考代碼中注釋: 

package test; 
 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
import java.util.Properties; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
import javax.mail.Address; 
import javax.mail.Authenticator; 
import javax.mail.BodyPart; 
import javax.mail.Message; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 
import com.sun.mail.util.MailSSLSocketFactory; 
 
///** 
// * 
// * @author tuzongxun123 
// * @Description 郵件發(fā)送測試類 
// */ 
public class sendMailTest { 
 public static void main(String[] args) throws Exception { 
 // 配置信息 
 Properties pro = new Properties(); 
 pro.put("mail.smtp.host", "smtp.163.com"); 
 pro.put("mail.smtp.auth", "true"); 
 // SSL加密 
 MailSSLSocketFactory sf = null; 
 sf = new MailSSLSocketFactory(); 
 // 設(shè)置信任所有的主機(jī) 
 sf.setTrustAllHosts(true); 
 pro.put("mail.smtp.ssl.enable", "true"); 
 pro.put("mail.smtp.ssl.socketFactory", sf); 
 // 根據(jù)郵件的會(huì)話屬性構(gòu)造一個(gè)發(fā)送郵件的Session,這里需要注意的是用戶名那里不能加后綴,否則便不是用戶名了 
 //還需要注意的是,這里的密碼不是正常使用郵箱的登陸密碼,而是客戶端生成的另一個(gè)專門的授權(quán)碼 
 MailAuthenticator authenticator = new MailAuthenticator("tuzongxun123", 
  "客戶端授權(quán)碼"); 
 Session session = Session.getInstance(pro, authenticator); 
 // 根據(jù)Session 構(gòu)建郵件信息 
 Message message = new MimeMessage(session); 
 // 創(chuàng)建郵件發(fā)送者地址 
 Address from = new InternetAddress("tuzongxun123@163.com"); 
 // 設(shè)置郵件消息的發(fā)送者 
 message.setFrom(from); 
 // 驗(yàn)證收件人郵箱地址 
 List<String> toAddressList = new ArrayList<>(); 
 toAddressList.add("1160569243@qq.com"); 
 StringBuffer buffer = new StringBuffer(); 
 if (!toAddressList.isEmpty()) { 
  String regEx = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; 
  Pattern p = Pattern.compile(regEx); 
  for (int i = 0; i < toAddressList.size(); i++) { 
  Matcher match = p.matcher(toAddressList.get(i)); 
  if (match.matches()) { 
   buffer.append(toAddressList.get(i)); 
   if (i < toAddressList.size() - 1) { 
   buffer.append(","); 
   } 
  } 
  } 
 } 
 String toAddress = buffer.toString(); 
 if (!toAddress.isEmpty()) { 
  // 創(chuàng)建郵件的接收者地址 
  Address[] to = InternetAddress.parse(toAddress); 
  // 設(shè)置郵件接收人地址 
  message.setRecipients(Message.RecipientType.TO, to); 
  // 郵件主題 
  // message.setSubject("java郵件測試"); 
  message.setSubject("為什么錯(cuò)了"); 
  // 郵件容器 
  MimeMultipart mimeMultiPart = new MimeMultipart(); 
  // 設(shè)置HTML 
  BodyPart bodyPart = new MimeBodyPart(); 
  // 郵件內(nèi)容 
  // String htmlText = "java郵件測試111"; 
  String htmlText = "為什么錯(cuò)了"; 
  bodyPart.setContent(htmlText, "text/html;charset=utf-8"); 
  mimeMultiPart.addBodyPart(bodyPart); 
  // 添加附件 
  List<String> fileAddressList = new ArrayList<String>(); 
  fileAddressList 
   .add("C:\\Users\\tuzongxun123\\Desktop\\新建 Microsoft Office Word 文檔.docx"); 
  if (fileAddressList != null) { 
  BodyPart attchPart = null; 
  for (int i = 0; i < fileAddressList.size(); i++) { 
   if (!fileAddressList.get(i).isEmpty()) { 
   attchPart = new MimeBodyPart(); 
   // 附件數(shù)據(jù)源 
   DataSource source = new FileDataSource( 
    fileAddressList.get(i)); 
   // 將附件數(shù)據(jù)源添加到郵件體 
   attchPart.setDataHandler(new DataHandler(source)); 
   // 設(shè)置附件名稱為原文件名 
   attchPart.setFileName(MimeUtility.encodeText(source 
    .getName())); 
   mimeMultiPart.addBodyPart(attchPart); 
   } 
  } 
  } 
  message.setContent(mimeMultiPart); 
  message.setSentDate(new Date()); 
  // 保存郵件 
  message.saveChanges(); 
  // 發(fā)送郵件 
  Transport.send(message); 
 } 
 } 
} 
 
class MailAuthenticator extends Authenticator { 
 
 /** 
 * 用戶名 
 */ 
 private String username; 
 /** 
 * 密碼 
 */ 
 private String password; 
 
 /** 
 * 創(chuàng)建一個(gè)新的實(shí)例 MailAuthenticator. 
 * 
 * @param username 
 * @param password 
 */ 
 public MailAuthenticator(String username, String password) { 
 this.username = username; 
 this.password = password; 
 } 
 
 public String getPassword() { 
 return password; 
 } 
 
 @Override 
 protected PasswordAuthentication getPasswordAuthentication() { 
 return new PasswordAuthentication(username, password); 
 } 
 
 public String getUsername() { 
 return username; 
 } 
 
 public void setPassword(String password) { 
 this.password = password; 
 } 
 
 public void setUsername(String username) { 
 this.username = username; 
 } 
 
} 

注:我有個(gè)同事使用我這個(gè)代碼更換為他的賬號(hào)和客戶端授權(quán)碼后,一運(yùn)行就報(bào)錯(cuò),然后重置了一下郵箱的客戶端授權(quán)碼后,錯(cuò)誤便消失了。

以上就是本文的全部內(nèi)容,希望對(duì)大家學(xué)習(xí)java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • SpringBoot實(shí)現(xiàn)線程池

    SpringBoot實(shí)現(xiàn)線程池

    現(xiàn)在由于系統(tǒng)越來越復(fù)雜,導(dǎo)致很多接口速度變慢,這時(shí)候就會(huì)想到可以利用線程池來處理一些耗時(shí)并不影響系統(tǒng)的操作。本文就介紹了SpringBoot線程池的使用,感興趣的可以了解一下
    2021-06-06
  • 使用IntelliJ IDEA調(diào)式Stream流的方法步驟

    使用IntelliJ IDEA調(diào)式Stream流的方法步驟

    本文主要介紹了使用IntelliJ IDEA調(diào)式Stream流的方法步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Struts2實(shí)現(xiàn)上傳單個(gè)文件功能

    Struts2實(shí)現(xiàn)上傳單個(gè)文件功能

    這篇文章主要為大家詳細(xì)介紹了Struts2實(shí)現(xiàn)上傳單個(gè)文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • MyBatis與Spring整合過程實(shí)例解析

    MyBatis與Spring整合過程實(shí)例解析

    這篇文章主要介紹了MyBatis與Spring整合過程實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • CountDownLatch同步工具類使用詳解

    CountDownLatch同步工具類使用詳解

    這篇文章主要為大家詳細(xì)介紹了CountDownLatch的使用說明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • java使用Logback配置輸出日志內(nèi)容到文件示例代碼

    java使用Logback配置輸出日志內(nèi)容到文件示例代碼

    這篇文章主要介紹了java?Logback輸出日志內(nèi)容到文件,要將logger.info的信息輸出到文件,您可以使用Logback配置,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • SpringBoot2.x設(shè)置Session失效時(shí)間及失效跳轉(zhuǎn)方式

    SpringBoot2.x設(shè)置Session失效時(shí)間及失效跳轉(zhuǎn)方式

    這篇文章主要介紹了SpringBoot2.x設(shè)置Session失效時(shí)間及失效跳轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 在SpringBoot項(xiàng)目中實(shí)現(xiàn)給所有請(qǐng)求加固定前綴

    在SpringBoot項(xiàng)目中實(shí)現(xiàn)給所有請(qǐng)求加固定前綴

    這篇文章主要介紹了在SpringBoot項(xiàng)目中實(shí)現(xiàn)給所有請(qǐng)求加固定前綴,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • ExecutorService實(shí)現(xiàn)獲取線程返回值

    ExecutorService實(shí)現(xiàn)獲取線程返回值

    這篇文章主要介紹了ExecutorService實(shí)現(xiàn)獲取線程返回值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Spring(一):IOC如何推導(dǎo)和理解

    Spring(一):IOC如何推導(dǎo)和理解

    下面小編就為大家?guī)硪黄斦凷pring對(duì)IOC的理解(推薦篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-07-07

最新評(píng)論