[收藏] 使用javamail發(fā)信過程中的一些問題及解決方法
1 今天在研究javamail發(fā)信的過程中,出現(xiàn)了一些小問題,現(xiàn)總結(jié)如下,以免后來者走些不必要的彎路,先把完整的能夠正常運行的代碼示例粘貼如下:
2 發(fā)郵件源代碼:
3 package com.hyq.test;
4
5 import java.util.Properties;
6 import javax.mail.*;
7 import javax.mail.internet.*;
8
9 public class MailExample {
10
11 public static void main (String args[]) throws Exception {
12
13 String host = "smtp.163.com"; //發(fā)件人使用發(fā)郵件的電子信箱服務(wù)器
14 String from = "你自己的電子信箱"; //發(fā)郵件的出發(fā)地(發(fā)件人的信箱)
15 String to = "收件人信箱"; //發(fā)郵件的目的地(收件人信箱)
16
17 // Get system properties
18 Properties props = System.getProperties();
19
20 // Setup mail server
21 props.put("mail.smtp.host", host);
22
23 // Get session
24 props.put("mail.smtp.auth", "true"); //這樣才能通過驗證
25
26 MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");
27 Session session = Session.getDefaultInstance(props, myauth);
28
29 // session.setDebug(true);
30
31 // Define message
32 MimeMessage message = new MimeMessage(session);
33
34
35 // Set the from address
36 message.setFrom(new InternetAddress(from));
37
38 // Set the to address
39 message.addRecipient(Message.RecipientType.TO,
40 new InternetAddress(to));
41
42 // Set the subject
43 message.setSubject("測試程序!");
44
45 // Set the content
46 message.setText("這是用java寫的發(fā)送電子郵件的測試程序!");
47
48 message.saveChanges();
49
50 Transport.send(message);
51
52 }
53 }
54
55 校驗發(fā)信人權(quán)限的方法
56 package com.hyq.test;
57
58 import javax.mail.PasswordAuthentication;
59
60 class MyAuthenticator
61 extends javax.mail.Authenticator {
62 private String strUser;
63 private String strPwd;
64 public MyAuthenticator(String user, String password) {
65 this.strUser = user;
66 this.strPwd = password;
67 }
68
69 protected PasswordAuthentication getPasswordAuthentication() {
70 return new PasswordAuthentication(strUser, strPwd);
71 }
72 }
73
74
75 注意:上面的事例僅為使用163信箱時發(fā)送電子郵件的方法,因為使用的host為:smtp.163.com,如源代碼中:String host = "smtp.163.com"; //發(fā)件人使用發(fā)郵件的電子信箱服務(wù)器,如果使用其它的電子郵件發(fā)送,就必須在其郵件服務(wù)器上查找相應(yīng)的電子郵件服務(wù)器,例如搜狐就要使用smtp.sohu.com,具體情況具體對待,都可以從所使用的郵件服務(wù)器上獲得的。如果沒有使用host ,也就是說,沒有進行props.put("mail.smtp.host", host);設(shè)置,那么就會拋 javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;的異常。當(dāng)然了,如果你沒有正確配置,這個異常仍然會被拋出的。
76
77 有些郵件服務(wù)系統(tǒng)是不需要驗證發(fā)件人的授權(quán)的,所以可以很簡單的使用
78 Session session = Session.getDefaultInstance(props, null);
79 而不必使用
80 props.put("mail.smtp.auth", "true");
81 MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");
82 Session session = Session.getDefaultInstance(props, myauth);
83
84 就可以發(fā)送電子郵件了,這個多為一些企事業(yè)單位的內(nèi)部電子信箱系統(tǒng)。
85 但是對于很多門戶網(wǎng)站上的電郵系統(tǒng),如:163,sohu,yahoo等等,如果仍然簡單的這樣使用就會拋
86
87 com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxuAyCAfmnZE8BwtIA==.32705S2
88
89
90 at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
91
92 at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
93
94 at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
95
96 at javax.mail.Transport.send0(Transport.java:169)
97
98 at javax.mail.Transport.send(Transport.java:98)
99
100 這樣的異常,要求你必須進行授權(quán)校驗,它的目的就是阻止他人任意亂發(fā)郵件,也算是為了減少垃圾郵件的出現(xiàn)吧。這時候,我們就要使用
101 props.put("mail.smtp.auth", "true");
102 MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");
103 Session session = Session.getDefaultInstance(props, myauth);
104
105 這里還有一個特別注意的事情:在你使用Session.getDefaultInstance時,一定要將 props.put ("mail.smtp.auth", "true"); 置為true,它默認的是false,如果你沒有做這一步,雖然你使用了 Session.getDefaultInstance(props, myauth);,你自己也確實 MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼 ");但是它仍然會拋出
106 com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxJA2SBrm3ZEFv0gIA==.40815S2
107
108
109 at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
110
111 at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
112
113 at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
114
115 at javax.mail.Transport.send0(Transport.java:169)
116
117 at javax.mail.Transport.send(Transport.java:98)
118 這樣的異常。我就在這一步費了好長時間,后來才發(fā)現(xiàn)了這個問題,很是郁悶。不過還好,總算解決了。
119
120 其實上面的做法只是比較簡單的一種,也有很多其它的寫法,如:
121 Properties props = System.getProperties();可以使用
122 Properties props = new Properties();來代替。
123
124
125 Transport.send(message);可以使用下面的代碼來代替
126 String username = "你的電子信箱用戶名";
127 String password = "你的電子信箱密碼";
128 message.saveChanges(); // implicit with send()
129 Transport transport = session.getTransport("smtp");
130 transport.connect("mail.htf.com.cn", username, password);
131 transport.sendMessage(message, message.getAllRecipients());
132 transport.close();
133 這種方法在同時發(fā)送多封電子郵件時比較有用。
134
135 還有一些具體的相關(guān)概念,可以查看相關(guān)的官方文檔,在我查詢資料時,發(fā)現(xiàn)了一篇文章寫得相當(dāng)仔細,可以加以參考:http://www.matrix.org.cn/resource/article/44/44101_JavaMail.html
136
137 另附上使用org.apache.commons.mail進行發(fā)電子郵件的示例:
138 import org.apache.commons.mail.SimpleEmail;
139 import org.apache.commons.mail.*;
140
141 public class TestCommon {
142 public TestCommon() {
143 }
144 public static void main(String[] args){
145 SimpleEmail email = new SimpleEmail();
146 email.setHostName("smtp.163.com");//設(shè)置使用發(fā)電子郵件的郵件服務(wù)器
147 try {
148 email.addTo("收件人信箱");
149 email.setAuthentication("發(fā)件人信箱","發(fā)件人信箱密碼");
150 email.setFrom("發(fā)件人信箱");
151 email.setSubject("Test apache.commons.mail message");
152 email.setMsg("This is a simple test of commons-email");
153 email.send();
154 }
155 catch (EmailException ex) {
156 ex.printStackTrace();
157 }
158 }
159 }
注:文章來自http://garyea.javaeye.com/blog/76460
2 發(fā)郵件源代碼:
3 package com.hyq.test;
4
5 import java.util.Properties;
6 import javax.mail.*;
7 import javax.mail.internet.*;
8
9 public class MailExample {
10
11 public static void main (String args[]) throws Exception {
12
13 String host = "smtp.163.com"; //發(fā)件人使用發(fā)郵件的電子信箱服務(wù)器
14 String from = "你自己的電子信箱"; //發(fā)郵件的出發(fā)地(發(fā)件人的信箱)
15 String to = "收件人信箱"; //發(fā)郵件的目的地(收件人信箱)
16
17 // Get system properties
18 Properties props = System.getProperties();
19
20 // Setup mail server
21 props.put("mail.smtp.host", host);
22
23 // Get session
24 props.put("mail.smtp.auth", "true"); //這樣才能通過驗證
25
26 MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");
27 Session session = Session.getDefaultInstance(props, myauth);
28
29 // session.setDebug(true);
30
31 // Define message
32 MimeMessage message = new MimeMessage(session);
33
34
35 // Set the from address
36 message.setFrom(new InternetAddress(from));
37
38 // Set the to address
39 message.addRecipient(Message.RecipientType.TO,
40 new InternetAddress(to));
41
42 // Set the subject
43 message.setSubject("測試程序!");
44
45 // Set the content
46 message.setText("這是用java寫的發(fā)送電子郵件的測試程序!");
47
48 message.saveChanges();
49
50 Transport.send(message);
51
52 }
53 }
54
55 校驗發(fā)信人權(quán)限的方法
56 package com.hyq.test;
57
58 import javax.mail.PasswordAuthentication;
59
60 class MyAuthenticator
61 extends javax.mail.Authenticator {
62 private String strUser;
63 private String strPwd;
64 public MyAuthenticator(String user, String password) {
65 this.strUser = user;
66 this.strPwd = password;
67 }
68
69 protected PasswordAuthentication getPasswordAuthentication() {
70 return new PasswordAuthentication(strUser, strPwd);
71 }
72 }
73
74
75 注意:上面的事例僅為使用163信箱時發(fā)送電子郵件的方法,因為使用的host為:smtp.163.com,如源代碼中:String host = "smtp.163.com"; //發(fā)件人使用發(fā)郵件的電子信箱服務(wù)器,如果使用其它的電子郵件發(fā)送,就必須在其郵件服務(wù)器上查找相應(yīng)的電子郵件服務(wù)器,例如搜狐就要使用smtp.sohu.com,具體情況具體對待,都可以從所使用的郵件服務(wù)器上獲得的。如果沒有使用host ,也就是說,沒有進行props.put("mail.smtp.host", host);設(shè)置,那么就會拋 javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;的異常。當(dāng)然了,如果你沒有正確配置,這個異常仍然會被拋出的。
76
77 有些郵件服務(wù)系統(tǒng)是不需要驗證發(fā)件人的授權(quán)的,所以可以很簡單的使用
78 Session session = Session.getDefaultInstance(props, null);
79 而不必使用
80 props.put("mail.smtp.auth", "true");
81 MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");
82 Session session = Session.getDefaultInstance(props, myauth);
83
84 就可以發(fā)送電子郵件了,這個多為一些企事業(yè)單位的內(nèi)部電子信箱系統(tǒng)。
85 但是對于很多門戶網(wǎng)站上的電郵系統(tǒng),如:163,sohu,yahoo等等,如果仍然簡單的這樣使用就會拋
86
87 com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxuAyCAfmnZE8BwtIA==.32705S2
88
89
90 at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
91
92 at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
93
94 at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
95
96 at javax.mail.Transport.send0(Transport.java:169)
97
98 at javax.mail.Transport.send(Transport.java:98)
99
100 這樣的異常,要求你必須進行授權(quán)校驗,它的目的就是阻止他人任意亂發(fā)郵件,也算是為了減少垃圾郵件的出現(xiàn)吧。這時候,我們就要使用
101 props.put("mail.smtp.auth", "true");
102 MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼");
103 Session session = Session.getDefaultInstance(props, myauth);
104
105 這里還有一個特別注意的事情:在你使用Session.getDefaultInstance時,一定要將 props.put ("mail.smtp.auth", "true"); 置為true,它默認的是false,如果你沒有做這一步,雖然你使用了 Session.getDefaultInstance(props, myauth);,你自己也確實 MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱", "你自己的信箱密碼 ");但是它仍然會拋出
106 com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxJA2SBrm3ZEFv0gIA==.40815S2
107
108
109 at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
110
111 at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
112
113 at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
114
115 at javax.mail.Transport.send0(Transport.java:169)
116
117 at javax.mail.Transport.send(Transport.java:98)
118 這樣的異常。我就在這一步費了好長時間,后來才發(fā)現(xiàn)了這個問題,很是郁悶。不過還好,總算解決了。
119
120 其實上面的做法只是比較簡單的一種,也有很多其它的寫法,如:
121 Properties props = System.getProperties();可以使用
122 Properties props = new Properties();來代替。
123
124
125 Transport.send(message);可以使用下面的代碼來代替
126 String username = "你的電子信箱用戶名";
127 String password = "你的電子信箱密碼";
128 message.saveChanges(); // implicit with send()
129 Transport transport = session.getTransport("smtp");
130 transport.connect("mail.htf.com.cn", username, password);
131 transport.sendMessage(message, message.getAllRecipients());
132 transport.close();
133 這種方法在同時發(fā)送多封電子郵件時比較有用。
134
135 還有一些具體的相關(guān)概念,可以查看相關(guān)的官方文檔,在我查詢資料時,發(fā)現(xiàn)了一篇文章寫得相當(dāng)仔細,可以加以參考:http://www.matrix.org.cn/resource/article/44/44101_JavaMail.html
136
137 另附上使用org.apache.commons.mail進行發(fā)電子郵件的示例:
138 import org.apache.commons.mail.SimpleEmail;
139 import org.apache.commons.mail.*;
140
141 public class TestCommon {
142 public TestCommon() {
143 }
144 public static void main(String[] args){
145 SimpleEmail email = new SimpleEmail();
146 email.setHostName("smtp.163.com");//設(shè)置使用發(fā)電子郵件的郵件服務(wù)器
147 try {
148 email.addTo("收件人信箱");
149 email.setAuthentication("發(fā)件人信箱","發(fā)件人信箱密碼");
150 email.setFrom("發(fā)件人信箱");
151 email.setSubject("Test apache.commons.mail message");
152 email.setMsg("This is a simple test of commons-email");
153 email.send();
154 }
155 catch (EmailException ex) {
156 ex.printStackTrace();
157 }
158 }
159 }