跟之前不同的地方在於,要透過 Exchange Server 好像必須提供 Authenticator,程式碼必須修改以下的部份:
1、 增加 Authenticator
// Authenticator used for creating mail session
public class SMTPAuthenticator extends javax.mail.Authenticator {
private String fUser = null;
private String fPassword = null;
public SMTPAuthenticator(String user, String password) {
fUser = user;
fPassword = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fUser, fPassword);
}
}2、要寄信時,產生 Mail Session 必須附上 Authenticator 作為引數
// Initial properties
java.util.Properties props = null;
..........
// Initial Authenticator
javax.mail.Authenticator smtpAuthenticator = new SMTPAuthenticator("account", "password");
// Put Properties and Authenticator as parameters for crating Mail Session
javax.mail.Session mailSession = javax.mail.Session.getInstance(props, smtpAuthenticator);
另外根據官方文件的說明,登入的帳號可能必須輸入 domain_name/windows_account/mail_account 這樣的形式才能正確通過 Exchange Server 的帳號驗證。
2012/06/15 補充:
在我們去國內某間大公司 debug 的經驗..他們內部是用 Exchange Server,不過不知道哪個版本
登入時是用 domain_name/mail_account 作為登入帳號才可以正常用 JavaMail 連接 Exchange Server。
沒有留言:
張貼留言