【问题标题】:Use Javamail for accessing Microsoft Exchange mailboxes (IMAP, MS Exchange)使用 Javamail 访问 Microsoft Exchange 邮箱(IMAP、MS Exchange)
【发布时间】:2013-12-02 17:24:58
【问题描述】:

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

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

我的调试器出现异常。

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

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");

【问题讨论】:

  • 您的代码是绝对正确的(也许有些行根本不需要 - 验证器示例,因为您在连接中使用了用户名和密码 - 但不要担心)它在我们的交换环境中工作。您需要与您的 Exchange 系统管理员交谈,因为问题应该来自那方面。
  • 抱歉更新晚了。是的,这段代码是正确的。问题出在 Exchange Server 上。如果有人打算使用此代码,则此代码是正确的..
  • 您能否描述一下您的 Exchange Server 在收到此错误时遇到的问题。谢谢!
  • 这很简单,我得到了错误的凭据。 @ClickerTweeker

标签: java authentication exchange-server jakarta-mail imap


【解决方案1】:

如果用户名和邮件ID都相同,它将进行锻炼。

例如:系统用户名为 john,邮件 id 为 john@test.com

IMAP 尝试使用来自 john@test.com 的用户名“john”登录邮件 id,如果用户 id 不同,则不会登录。

【讨论】:

    【解决方案2】:

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

    另外,请参阅common mistakes 列表。

    【讨论】:

      【解决方案3】:

      尝试使用您自己的身份验证器

      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);
          }
      }
      

      并在您的代码中添加

      session = Session.getInstance(props,new ExchangeAuthenticator(username, password));
      

      【讨论】:

        猜你喜欢
        • 2012-07-27
        • 2011-03-28
        • 2016-10-17
        • 2011-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        相关资源
        最近更新 更多