使用javamail發信過程中的一些問題及解決方法
今天在研究javamail發信的過程中,出現了一些小問題,現總結如下,以免後來者走些不必要的彎路,先把完整的能夠正常執行的程式碼示例貼上如下:
發郵件原始碼:
package com.hyq.test;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class MailExample {
public static void main (String args[]) throws Exception {
String host = "smtp.163.com"; //發件人使用發郵件的電子信箱伺服器
String from = "你自己的電子信箱"; //發郵件的出發地(發件人的信箱)
String to = "收件人信箱"; //發郵件的目的地(收件人信箱)
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
props.put("mail.smtp.auth", "true"); //這樣才能通過驗證
MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");
Session session = Session.getDefaultInstance(props, myauth);
// session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set the subject
message.setSubject("測試程式!");
// Set the content
message.setText("這是用java寫的傳送電子郵件的測試程式!");
message.saveChanges();
Transport.send(message);
}
}
校驗發信人許可權的方法
package com.hyq.test;
import javax.mail.PasswordAuthentication;
class MyAuthenticator
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
注意:上面的事例僅為使用163信箱時傳送電子郵件的方法,因為使用的host為:smtp.163.com,如原始碼中:String host = "smtp.163.com"; //發件人使用發郵件的電子信箱伺服器,如果使用其它的電子郵件傳送,就必須在其郵件伺服器上查詢相應的電子郵件伺服器,例如搜狐就要使用smtp.sohu.com,具體情況具體對待,都可以從所使用的郵件伺服器上獲得的。如果沒有使用host ,也就是說,沒有進行props.put("mail.smtp.host", host);設定,那麼就會拋javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;的異常。當然了,如果你沒有正確配置,這個異常仍然會被丟擲的。
有些郵件服務系統是不需要驗證發件人的授權的,所以可以很簡單的使用
Session session = Session.getDefaultInstance(props, null);
而不必使用
props.put("mail.smtp.auth", "true");
MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");
Session session = Session.getDefaultInstance(props, myauth);
就可以傳送電子郵件了,這個多為一些企事業單位的內部電子信箱系統。
但是對於很多入口網站上的電郵系統,如:163,sohu,yahoo等等,如果仍然簡單的這樣使用就會拋
com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxuAyCAfmnZE8BwtIA==.32705S2
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
這樣的異常,要求你必須進行授權校驗,它的目的就是阻止他人任意亂髮郵件,也算是為了減少垃圾郵件的出現吧。這時候,我們就要使用
props.put("mail.smtp.auth", "true");
MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");
Session session = Session.getDefaultInstance(props, myauth);
這裡還有一個特別注意的事情:在你使用Session.getDefaultInstance時,一定要將 props.put("mail.smtp.auth", "true"); 置為true,它預設的是false,如果你沒有做這一步,雖然你使用了Session.getDefaultInstance(props, myauth);,你自己也確實 MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");但是它仍然會丟擲
com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxJA2SBrm3ZEFv0gIA==.40815S2
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
這樣的異常。我就在這一步費了好長時間,後來才發現了這個問題,很是鬱悶。不過還好,總算解決了。
其實上面的做法只是比較簡單的一種,也有很多其它的寫法,如:
Properties props = System.getProperties();可以使用
Properties props = new Properties();來代替。
Transport.send(message);可以使用下面的程式碼來代替
String username = "你的電子信箱使用者名稱";
String password = "你的電子信箱密碼";
message.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
transport.connect("mail.htf.com.cn", username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
這種方法在同時傳送多封電子郵件時比較有用。
還有一些具體的相關概念,可以檢視相關的官方文件,在我查詢資料時,發現了一篇文章寫得相當仔細,可以加以參考:http://www.matrix.org.cn/resource/article/44/44101_JavaMail.html
另附上使用org.apache.commons.mail進行發電子郵件的示例:
import org.apache.commons.mail.SimpleEmail;
import org.apache.commons.mail.*;
public class TestCommon {
public TestCommon() {
}
public static void main(String[] args){
SimpleEmail email = new SimpleEmail();
email.setHostName("smtp.163.com");//設定使用發電子郵件的郵件伺服器
try {
email.addTo("收件人信箱");
email.setAuthentication("發件人信箱","發件人信箱密碼");
email.setFrom("發件人信箱");
email.setSubject("Test apache.commons.mail message");
email.setMsg("This is a simple test of commons-email");
email.send();
}
catch (EmailException ex) {
ex.printStackTrace();
}
}
}
發郵件原始碼:
package com.hyq.test;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class MailExample {
public static void main (String args[]) throws Exception {
String host = "smtp.163.com"; //發件人使用發郵件的電子信箱伺服器
String from = "你自己的電子信箱"; //發郵件的出發地(發件人的信箱)
String to = "收件人信箱"; //發郵件的目的地(收件人信箱)
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
props.put("mail.smtp.auth", "true"); //這樣才能通過驗證
MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");
Session session = Session.getDefaultInstance(props, myauth);
// session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set the subject
message.setSubject("測試程式!");
// Set the content
message.setText("這是用java寫的傳送電子郵件的測試程式!");
message.saveChanges();
Transport.send(message);
}
}
校驗發信人許可權的方法
package com.hyq.test;
import javax.mail.PasswordAuthentication;
class MyAuthenticator
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
注意:上面的事例僅為使用163信箱時傳送電子郵件的方法,因為使用的host為:smtp.163.com,如原始碼中:String host = "smtp.163.com"; //發件人使用發郵件的電子信箱伺服器,如果使用其它的電子郵件傳送,就必須在其郵件伺服器上查詢相應的電子郵件伺服器,例如搜狐就要使用smtp.sohu.com,具體情況具體對待,都可以從所使用的郵件伺服器上獲得的。如果沒有使用host ,也就是說,沒有進行props.put("mail.smtp.host", host);設定,那麼就會拋javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;的異常。當然了,如果你沒有正確配置,這個異常仍然會被丟擲的。
有些郵件服務系統是不需要驗證發件人的授權的,所以可以很簡單的使用
Session session = Session.getDefaultInstance(props, null);
而不必使用
props.put("mail.smtp.auth", "true");
MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");
Session session = Session.getDefaultInstance(props, myauth);
就可以傳送電子郵件了,這個多為一些企事業單位的內部電子信箱系統。
但是對於很多入口網站上的電郵系統,如:163,sohu,yahoo等等,如果仍然簡單的這樣使用就會拋
com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxuAyCAfmnZE8BwtIA==.32705S2
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
這樣的異常,要求你必須進行授權校驗,它的目的就是阻止他人任意亂髮郵件,也算是為了減少垃圾郵件的出現吧。這時候,我們就要使用
props.put("mail.smtp.auth", "true");
MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");
Session session = Session.getDefaultInstance(props, myauth);
這裡還有一個特別注意的事情:在你使用Session.getDefaultInstance時,一定要將 props.put("mail.smtp.auth", "true"); 置為true,它預設的是false,如果你沒有做這一步,雖然你使用了Session.getDefaultInstance(props, myauth);,你自己也確實 MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");但是它仍然會丟擲
com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxJA2SBrm3ZEFv0gIA==.40815S2
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
這樣的異常。我就在這一步費了好長時間,後來才發現了這個問題,很是鬱悶。不過還好,總算解決了。
其實上面的做法只是比較簡單的一種,也有很多其它的寫法,如:
Properties props = System.getProperties();可以使用
Properties props = new Properties();來代替。
Transport.send(message);可以使用下面的程式碼來代替
String username = "你的電子信箱使用者名稱";
String password = "你的電子信箱密碼";
message.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
transport.connect("mail.htf.com.cn", username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
這種方法在同時傳送多封電子郵件時比較有用。
還有一些具體的相關概念,可以檢視相關的官方文件,在我查詢資料時,發現了一篇文章寫得相當仔細,可以加以參考:http://www.matrix.org.cn/resource/article/44/44101_JavaMail.html
另附上使用org.apache.commons.mail進行發電子郵件的示例:
import org.apache.commons.mail.SimpleEmail;
import org.apache.commons.mail.*;
public class TestCommon {
public TestCommon() {
}
public static void main(String[] args){
SimpleEmail email = new SimpleEmail();
email.setHostName("smtp.163.com");//設定使用發電子郵件的郵件伺服器
try {
email.addTo("收件人信箱");
email.setAuthentication("發件人信箱","發件人信箱密碼");
email.setFrom("發件人信箱");
email.setSubject("Test apache.commons.mail message");
email.setMsg("This is a simple test of commons-email");
email.send();
}
catch (EmailException ex) {
ex.printStackTrace();
}
}
}
相關文章
- workerman開發過程中遇到的一些常見的問題與解決方法
- elk(單機)安裝過程中遇到的問題及解決方法
- Oracle RAC之--安裝過程中碰到的問題及解決方法Oracle
- 近兩天學習使用 Homestead 過程中遇到的問題及解決方法
- dsp builder 11.0 使用過程中產生的問題及解決方案UI
- oracle 11.2.0.4 DataGuard Broker配置過程中可能遇到的問題及解決方法Oracle
- 在SQLServer處理中的一些問題及解決方法SQLServer
- Mysql安裝過程中遇到的問題及解決辦法MySql
- Linux使用過程中常見問題及其解決方法Linux
- goland中npm無法使用的問題及解決方法GoLandNPM
- JS中toFixed()方法的問題及解決方案JS
- JSP開發過程遇到的中文亂碼問題及解決方案JS
- RAC安裝配置和使用過程的問題解決方法總結一
- RAC安裝配置和使用過程的問題解決方法總結二
- 整理debian安裝過程中的一些問題與方法
- 快取過程存在的三大問題及解決方案快取
- 【mysql】配置MySQL,解決安裝過程中的問題MySql
- Composer 使用過程中遇到的問題和解決方案
- oracle 10g R2 安裝過程遇到的問題及解決方法Oracle 10g
- 使用HttpClient過程中常見的一些問題HTTPclient
- tensorflow安裝使用過程錯誤及解決方法
- 記一次wordpress安裝過程中遇到的問題及解決辦法
- 一個lua問題解決過程
- 接續:RAC安裝配置和使用過程的問題解決方法總結二
- Vue專案開發過程中遇到的一些問題總結Vue
- 【轉】npm使用過程中的一些錯誤解決辦法及npm常用命令NPM
- aix5.3 + oracle9206 建庫過程中遇到的問題及解決AIOracle
- [經驗]iOS開發-記錄下在開發過程中遇到的問題的解決方案及經驗總結-1iOS
- 無線路由器撥號過程中可能會遇到的問題以及解決方法路由器
- 解決ADSL連線過程中資料流傳輸突然中斷問題的方法
- 配置IIS,Apache,PHP過程中遇到的一些問題ApachePHP
- npm使用過程中的一些錯誤解決辦法及npm常用命令和技巧NPM
- 教育直播平臺開發過程中,這些技術問題需要解決
- 編譯移植Mplayer到mini2440開發板的過程及問題解決辦法編譯
- android開發過程中遇到的問題Android
- 專案管理過程中的問題分析方法(轉)專案管理
- Pentaho 使用中發現的幾個問題和解決方法
- 解決Java執行過程中拋簽名異常的問題Java