1. 程式人生 > >java mail 傳送郵件

java mail 傳送郵件

程式碼

package test.smtp;

import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Date; import java.util.Properties;

public class Send {     // 發件人的 郵箱 和 密碼(替換為自己的郵箱和密碼)     // PS: 某些郵箱伺服器為了增加郵箱本身密碼的安全性,給 SMTP 客戶端設定了獨立密碼(有的郵箱稱為“授權碼”),      //     對於開啟了獨立密碼的郵箱, 這裡的郵箱密碼必需使用這個獨立密碼(授權碼)。     public static String myEmailAccount = "

[email protected]";     public static String myEmailPassword = "shouquanma";

    // 發件人郵箱的 SMTP 伺服器地址, 必須準確, 不同郵件伺服器地址不同, 一般(只是一般, 絕非絕對)格式為: smtp.xxx.com     // 網易163郵箱的 SMTP 伺服器地址為: smtp.163.com     public static String myEmailSMTPHost = "smtp.qq.com";

    // 收件人郵箱(替換為自己知道的有效郵箱)     public static String receiveMailAccount = "

[email protected]";

    public static void main(String[] args) throws Exception {                  String host = myEmailSMTPHost;         int port = 25;         String password = myEmailPassword;         String username = myEmailAccount;

        // 配置傳送郵件的環境屬性         final Properties props = new Properties();         // 表示SMTP傳送郵件,需要進行身份驗證         props.put("mail.smtp.auth", "true");         props.put("mail.smtp.host", host);         props.put("mail.smtp.port", port);         props.put("mail.smtp.timeout", "10000");         // 如果使用ssl,則去掉使用25埠的配置,進行如下配置,         // props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");         // props.put("mail.smtp.socketFactory.port", "465");         // props.put("mail.smtp.port", "465");

        // 發件人的賬號         props.put("mail.user", username);         // 訪問SMTP服務時需要提供的密碼         props.put("mail.password", password);

        // 構建授權資訊,用於進行SMTP進行身份驗證         Authenticator authenticator = new Authenticator() {             @Override             protected PasswordAuthentication getPasswordAuthentication() {                 // 使用者名稱、密碼                 String userName = props.getProperty("mail.user");                 String password = props.getProperty("mail.password");                 return new PasswordAuthentication(userName, password);             }         };         // 使用環境屬性和授權資訊,建立郵件會話         Session mailSession = Session.getInstance(props, authenticator);         // 建立郵件訊息         MimeMessage message = new MimeMessage(mailSession);         try {             // 設定發件人             InternetAddress form = new InternetAddress(                     props.getProperty("mail.user"));             message.setFrom(form);             // 設定收件人             InternetAddress to = new InternetAddress(receiveMailAccount);//給自己傳送一封驗證郵件             message.setRecipient(MimeMessage.RecipientType.TO, to);             // 設定郵件標題             message.setSubject("SMTP配置驗證");             // 設定郵件的內容體             message.setContent("當收到該郵件時,說明您已經正確配置SMTP郵件傳送服務。", "text/html;charset=UTF-8");             // 傳送郵件             Transport.send(message);         }         catch (Exception e){             e.printStackTrace();         }     } }

獲取QQ郵箱授權碼,設定》賬戶》開啟服務:POP3/SMTP服務,點選開啟時會要求通過手機驗證,驗證完之後會生成一個授權碼,郵箱登入程式碼中的認證密碼必須為授權碼,見上面程式碼註釋