使用 Javamail 访问 Microsoft Exchange 邮箱(IMAP、MS Exchange)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20066802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use Javamail for accessing Microsoft Exchange mailboxes (IMAP, MS Exchange)
提问by Dilanga Thalpegama
I need to connect to a Microsoft Exchange Server through IMAPS JavaMail. First, I got the:
我需要通过 IMAPS JavaMail 连接到 Microsoft Exchange Server。首先,我得到了:
A1 NO AUTHENTICATE failed.
javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
exception in my debugger.
我的调试器中的异常。
Then, I disabled some authentication protocols:
然后,我禁用了一些身份验证协议:
imapProps.setProperty("mail.imaps.auth.plain.disable", "true");
imapProps.setProperty("mail.imaps.auth.ntlm.disable", "true");
imapProps.setProperty("mail.imaps.auth.gssapi.disable", "true");
This is the new exception I'm getting (I've attached the whole log):
这是我得到的新异常(我附上了整个日志):
DEBUG: setDebug: JavaMail version 1.4.4
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: disable AUTH=PLAIN
DEBUG: disable AUTH=NTLM
DEBUG: enable STARTTLS
DEBUG: trying to connect to host "host.domain.com", port 993, isSSL true
* OK The Microsoft Exchange IMAP4 service is ready.
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAP: AUTH: NTLM
DEBUG IMAP: AUTH: GSSAPI
DEBUG IMAP: AUTH: PLAIN
DEBUG: protocolConnect login, host=host.domain.com, [email protected], password=<non-null>
A1 LOGIN [email protected] password
A1 NO LOGIN failed.
DEBUG: trying to connect to host "host.domain.com", port 993, isSSL true
* OK The Microsoft Exchange IMAP4 service is ready.
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAP: AUTH: NTLM
DEBUG IMAP: AUTH: GSSAPI
DEBUG IMAP: AUTH: PLAIN
DEBUG: protocolConnect login, host=host.domain.com, [email protected], password=<non-null>
A1 LOGIN [email protected] password
A1 NO LOGIN failed.
javax.mail.AuthenticationFailedException: LOGIN failed.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:660)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at ConnectMail.connectMail(ConnectMail.java:63)
at Main.main(Main.java:9)
Now I'm getting the "NO LOGIN failed" Exception.
现在我收到“NO LOGIN failed”异常。
This is my full code:
这是我的完整代码:
Properties imapProps = new Properties();
imapProps.setProperty("mail.imaps.socketFactory.port", "993");
imapProps.setProperty("mail.imaps.starttls.enable", "true");
imapProps.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
imapProps.setProperty("mail.imaps.socketFactory.fallback", "false");
imapProps.setProperty("mail.imaps.auth.plain.disable", "true");
imapProps.setProperty("mail.imaps.auth.ntlm.disable", "true");
imapProps.setProperty("mail.imaps.auth.gssapi.disable", "true");
imapProps.setProperty("username", "[email protected]");
imapProps.setProperty("password", "password");
String host = "host.domain.com";
int port = Integer.parseInt("993");
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "password");
}
};
session = Session.getInstance(imapProps, authenticator);
session.setDebug(true);
Store store = session.getStore("imaps");
store.connect(host, "[email protected]", "password");
回答by Sandhu Santhakumar
Try using your own Authenticator
尝试使用您自己的身份验证器
public class ExchangeAuthenticator extends Authenticator {
String username;
String password;
public ExchangeAuthenticator(String username, String password) {
super();
this.username= username;
this.password= password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
and in your code add
并在您的代码中添加
session = Session.getInstance(props,new ExchangeAuthenticator(username, password));
回答by Bill Shannon
You need to use a newer version of JavaMail that supports NTLM authentication. The latest version is 1.5.1.
您需要使用支持 NTLM 身份验证的较新版本的 JavaMail。最新版本是1.5.1。
Also, see this list of common mistakes.
另请参阅此常见错误列表。
回答by user10512315
It will workout if the username and the mail id both are same.
如果用户名和邮件 id 都相同,它就会锻炼。
For example: system username is john and mail id [email protected]
例如:系统用户名是 john,邮件 id 是 [email protected]
IMAP is try to login the mail id using the username "john" from [email protected], if the user id is differ it will not login.
IMAP 尝试使用来自 [email protected] 的用户名“john”登录邮件 ID,如果用户 ID 不同,则不会登录。