亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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(); 
 // 設置信任所有的主機 
 sf.setTrustAllHosts(true); 
 pro.put("mail.smtp.ssl.enable", "true"); 
 pro.put("mail.smtp.ssl.socketFactory", sf); 
 // 根據(jù)郵件的會話屬性構造一個發(fā)送郵件的Session,這里需要注意的是用戶名那里不能加后綴,否則便不是用戶名了 
 //還需要注意的是,這里的密碼不是正常使用郵箱的登陸密碼,而是客戶端生成的另一個專門的授權碼 
 MailAuthenticator authenticator = new MailAuthenticator("tuzongxun123", 
  "客戶端授權碼"); 
 Session session = Session.getInstance(pro, authenticator); 
 // 根據(jù)Session 構建郵件信息 
 Message message = new MimeMessage(session); 
 // 創(chuàng)建郵件發(fā)送者地址 
 Address from = new InternetAddress("tuzongxun123@163.com"); 
 // 設置郵件消息的發(fā)送者 
 message.setFrom(from); 
 // 驗證收件人郵箱地址 
 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); 
  // 設置郵件接收人地址 
  message.setRecipients(Message.RecipientType.TO, to); 
  // 郵件主題 
  // message.setSubject("java郵件測試"); 
  message.setSubject("為什么錯了"); 
  // 郵件容器 
  MimeMultipart mimeMultiPart = new MimeMultipart(); 
  // 設置HTML 
  BodyPart bodyPart = new MimeBodyPart(); 
  // 郵件內(nèi)容 
  // String htmlText = "java郵件測試111"; 
  String htmlText = "為什么錯了"; 
  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)); 
   // 設置附件名稱為原文件名 
   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)建一個新的實例 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; 
 } 
 
} 

注:我有個同事使用我這個代碼更換為他的賬號和客戶端授權碼后,一運行就報錯,然后重置了一下郵箱的客戶端授權碼后,錯誤便消失了。

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

相關文章

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

    SpringBoot實現(xiàn)線程池

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

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

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

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

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

    MyBatis與Spring整合過程實例解析

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

    CountDownLatch同步工具類使用詳解

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

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

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

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

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

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

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

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

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

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

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

最新評論