狂神說Java【網站註冊傳送郵件功能實現】—— (二) 簡單郵件傳送實現
阿新 • • 發佈:2020-09-09
郵件分類
-
簡單郵件:沒有除了文字以外的其他所有檔案(包括附件和圖片、視訊等),即純文字郵件
-
複雜郵件:除了傳統的文字資訊外,還包括了一些非文字資料的郵件
需要傳送郵件首先就要我們的郵箱賬號支援POP3和SMTP協議,所以我們需要開啟郵箱的POP3+SMTP服務,然後我們需要複製下圖中的授權碼,這個授權碼就相當於你的QQ密碼,你可以使用你的郵箱賬號+授權碼來發送郵件,而SMTP伺服器也就是使用這個來識別你的身份的
package com.thhh; import com.sun.mail.util.MailSSLSocketFactory; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; //傳送一封簡單的郵件 public class MailDemo01 { public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.setProperty("mail.host", "smtp.qq.com"); //設定QQ郵件伺服器 prop.setProperty("mail.transport.protocol", "smtp"); // 郵件傳送協議 prop.setProperty("mail.smtp.auth", "true"); // 需要驗證使用者名稱密碼 // 關於QQ郵箱,還要設定SSL加密,加上以下程式碼即可 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable", "true"); prop.put("mail.smtp.ssl.socketFactory", sf); //使用JavaMail傳送郵件的5個步驟 //1、建立定義整個應用程式所需的環境資訊的 Session 物件 //使用QQ郵箱的時候才需要,其他郵箱不需要這一段程式碼 Session session = Session.getDefaultInstance(prop, new Authenticator() {//獲取和SMTP伺服器的連線物件 public PasswordAuthentication getPasswordAuthentication() { //發件人郵件使用者名稱、授權碼 return new PasswordAuthentication("[email protected]", "授權碼"); } }); //開啟Session的debug模式,這樣就可以檢視到程式傳送Email的執行狀態 session.setDebug(true); //2、通過session得到transport物件 Transport ts = session.getTransport();//通過這一次和SMTP伺服器的連線物件獲取傳送郵件的傳輸物件 //3、使用郵箱的使用者名稱和授權碼連上SMTP郵件伺服器,即登陸 ts.connect("smtp.qq.com", "[email protected]", "授權碼"); //4、建立郵件物件MimeMessage——點選網頁上的寫信 //建立一個郵件物件 MimeMessage message = new MimeMessage(session); //指明郵件的發件人——在網頁上填寫發件人 message.setFrom(new InternetAddress("[email protected]"));//設定發件人 //指明郵件的收件人,現在發件人和收件人是一樣的,那就是自己給自己發——在網頁上填寫收件人 message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));//設定收件人 //郵件的標題——在網頁上填寫郵件標題 message.setSubject("簡單郵件傳送實現");//設定郵件主題 //郵件的文字內容——在網頁上填寫郵件內容 message.setContent("<h2 style='color:red'>你好啊!</h2>", "text/html;charset=UTF-8");//設定郵件的具體內容 //5、傳送郵件——在網頁上點擊發送按鈕 ts.sendMessage(message, message.getAllRecipients()); //6、關閉連線物件,即關閉伺服器上的連線資源 ts.close(); } }
執行測試
解析以下簡單郵件傳送的7個固定步驟,如果你要使用,直接複製貼上程式碼,並將你的郵箱+授權碼填進去就可以用了
1、準備一些引數
Properties prop = new Properties(); prop.setProperty("mail.host", "smtp.qq.com"); //設定QQ郵件伺服器 prop.setProperty("mail.transport.protocol", "smtp"); // 郵件傳送協議 prop.setProperty("mail.smtp.auth", "true"); // 需要驗證使用者名稱密碼 // 關於QQ郵箱,還要設定SSL加密,加上以下程式碼即可 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable", "true"); prop.put("mail.smtp.ssl.socketFactory", sf);
2、獲取session物件
//1、建立定義整個應用程式所需的環境資訊的 Session 物件 //使用QQ郵箱的時候才需要,其他郵箱不需要這一段程式碼 Session session = Session.getDefaultInstance(prop, new Authenticator() {//獲取和SMTP伺服器的連線物件 public PasswordAuthentication getPasswordAuthentication() { //發件人郵件使用者名稱、授權碼 return new PasswordAuthentication("[email protected]", "授權碼"); } }); //開啟Session的debug模式,這樣就可以檢視到程式傳送Email的執行狀態 session.setDebug(true); //不開DEBUG我們在控制檯就看不到它是怎麼傳送的郵件,只能等著程式結束再去郵箱中檢視是不是收到了郵件
3、獲取傳輸物件
//2、通過session得到transport物件
Transport ts = session.getTransport();//通過這一次和SMTP伺服器的連線物件獲取傳送郵件的傳輸物件
4、登陸授權
//3、使用郵箱的使用者名稱和授權碼連上SMTP郵件伺服器,即登陸
ts.connect("smtp.qq.com", "[email protected]", "授權碼");
5、寫郵件
//4、建立郵件物件MimeMessage——點選網頁上的寫信
//建立一個郵件物件
MimeMessage message = new MimeMessage(session);
//指明郵件的發件人——在網頁上填寫發件人
message.setFrom(new InternetAddress("[email protected]"));//設定發件人
//指明郵件的收件人,現在發件人和收件人是一樣的,那就是自己給自己發——在網頁上填寫收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));//設定收件人
//郵件的標題——在網頁上填寫郵件標題
message.setSubject("簡單郵件傳送實現");//設定郵件主題
//郵件的文字內容——在網頁上填寫郵件內容
message.setContent("<h2 style='color:red'>你好啊!</h2>", "text/html;charset=UTF-8");//設定郵件的具體內容
6、傳送郵件
//5、傳送郵件——在網頁上點擊發送按鈕
ts.sendMessage(message, message.getAllRecipients());
7、關閉伺服器資源
//6、關閉連線物件,即關閉程式中開啟的連線資源
ts.close();