java - 使用 Javamail 访问 Microsoft Exchange 邮箱(IMAP、MS Exchange)

标签 java authentication exchange-server jakarta-mail imap

我需要通过 IMAPS JavaMail 连接到 Microsoft Exchange Server。首先,我得到了:

A1 NO AUTHENTICATE failed.
javax.mail.AuthenticationFailedException: AUTHENTICATE failed.

我的调试器出现异常。

然后,我禁用了一些身份验证协议(protocol):

imapProps.setProperty("mail.imaps.auth.plain.disable", "true");
imapProps.setProperty("mail.imaps.auth.ntlm.disable", "true");
imapProps.setProperty("mail.imaps.auth.gssapi.disable", "true");

这是我得到的新异常(我附上了整个日志):

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, user=user@domain.com,  password=<non-null>
A1 LOGIN user@domain.com 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, user=user@domain.com, password=<non-null>
A1 LOGIN user@domain.com 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)

现在我收到“NO LOGIN failed”异常。

这是我的完整代码:

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", "user@domain.com");   
imapProps.setProperty("password", "password");

String host = "host.domain.com";
int port = Integer.parseInt("993");

Authenticator authenticator = new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("user@domain.com", "password");
    }
};

session = Session.getInstance(imapProps, authenticator);

session.setDebug(true);

Store store = session.getStore("imaps");

store.connect(host, "user@domain.com", "password");

最佳答案

您需要使用支持 NTLM 身份验证的较新版本的 JavaMail。最新版本是1.5.1 .

另请参阅 common mistakes 的列表.

关于java - 使用 Javamail 访问 Microsoft Exchange 邮箱(IMAP、MS Exchange),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20066802/

相关文章:

java - jasperreports-4.5.1.jar 包含哪些 jar

Python 3.X 玩转互联网

exchange-server - 在 Microsoft Exchange 中获取完整的用户列表

delphi - 在 MS Exchange Server 2007 中使用 [delphi] MadExcept 错误处理

c# - Exchange Web 服务 (EWS) 调用在 ASP.NET 下失败,但在控制台应用程序下失败

java - 子报表不显示

java - 如何从 java 代码将值赋给 shell 脚本中的变量

php - REST API 授权和身份验证(网络 + 移动)

python - 如何在没有 Oauth 的情况下登录 twitter 脚本?

java - 默认值 -1 的 spring transaction 超时时间是什么时候?