開發環境:jdk 1.6
開發工具:myeclipse10
package Email;
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import com.sun.mail.util.MailSSLSocketFactory;
public class Text1 extends Thread {
private String mailAdr;
private String content;
private String subject;
public Text1(String mailAdr, String subject, String content) {
// TODO Auto-generated constructor stub
super();
this.mailAdr = mailAdr;
this.subject = subject;
this.content = content;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
sendMail(mailAdr, subject, content);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendMail(String mailAdr, String subject, String content) throws Exception {
// TODO Auto-generated method stub
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
final Properties props = new Properties();
// 表示SMTP發送郵件,需要進行身份驗證
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.163.com");
// 端口
/* props.put("mail.smtp.port", 25); */
// smtp登陸的賬号、密碼 ;需開啟smtp登陸
props.setProperty("mail.debug", "true");
props.put("mail.user", "187********@163.com");
//smtp開啟後生成的許可碼
props.put("mail.password", "lll***");
//特别需要注意,要将ssl協定設定為true,否則會報530錯誤
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Authenticator authenticator = new Authenticator() {
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(mailAdr);
message.setRecipient(RecipientType.TO, to);
// 設定抄送
// InternetAddress cc = new InternetAddress("[email protected]");
// message.setRecipient(RecipientType.CC, cc);
// 設定密送,其他的收件人不能看到密送的郵件位址
// InternetAddress bcc = new InternetAddress("[email protected]");
// message.setRecipient(RecipientType.CC, bcc);
// 設定郵件标題
message.setSubject(subject);
// 設定郵件的内容體
message.setContent(content, "text/html;charset=UTF-8");
// 發送郵件
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Text1 d = new Text1("3955*****@qq.com", "測試郵件标題",
"測試郵件001 <br>内容....");
d.start();
System.out.println("程式繼續...");
}
}
代碼來自:https://blog.csdn.net/w286554018/article/details/78667002
問題:
如圖1所示,參考初始的https://blog.csdn.net/xuhailiang0816/article/details/78414211這篇文章,但是抛出
javax.mail.AuthenticationFailedException
查遍全網,都是SMTP設定的問題,密碼需使用設定後生成的許可碼,但是我再三确認沒有問題。
2,轉換思路,檢查自身防火牆,telnet smtp.163.com 25,沒有任何問題。
3,從頭開始,參考https://blog.csdn.net/w286554018/article/details/78667002代碼,抛出異常
沒有加載資源:/META-INF/javamail.providers
豁然開朗,此類解決方案很多,大抵因為JavaEE的中郵類與郵件包沖突,無法指向郵件包所緻,在JavaEE的中删掉郵件即可,問題解決。
4,異常抛出之是以不同,因為加入了props.setProperty(“mail.debug”,“true”),論異常準确抛出的重要性。
生産環境:office365,TLS加密方式
debug抛出:
Client was not authenticated to send anonymous mail during MAIL FROM
參考:https://blog.csdn.net/sinat_33324214/article/details/52910186
成功解決問題。