Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Javamail for accessing Microsoft Exchange mailboxes (IMAP, MS Exchange)

I need to connect to a Microsoft Exchange Server through IMAPS JavaMail. First, I got the:

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.

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");
like image 440
Dilanga Thalpegama Avatar asked Nov 19 '13 08:11

Dilanga Thalpegama


People also ask

How do I access a user mailbox in Exchange?

As a Microsoft 365 admin for Enterprises, Midsize, or Education, you can also access EAC by selecting Admin > Exchange in the Microsoft Online Portal. In the EAC, navigate to Recipients > Mailboxes. In the list of mailboxes, select the mailbox that you want to assign permissions for, and then select Edit .

How do I connect my email to Microsoft Exchange?

Within our app, go to Settings > Add Account > Add Email Account > Enter your email address and continue. If it does not automatically detect you are using an Exchange account, tap "not a ___ account" then tap the Exchange icon on the account setup page.


2 Answers

You need to use a newer version of JavaMail that supports NTLM authentication. The latest version is 1.5.1.

Also, see this list of common mistakes.

like image 98
Bill Shannon Avatar answered Oct 04 '22 04:10

Bill Shannon


It will workout if the username and the mail id both are same.

For example: system username is john and mail 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.

like image 22
user10512315 Avatar answered Oct 04 '22 02:10

user10512315