0
votes

first of all, sorry for my english (I'm from Spain)

I'm trying to connect to an Exchange 2010 mail server with IMAP and SSL from Javamail inside a Weblogic 8.1 server.

This is my code

Properties prop = new Properties();

// IMAP
// Disable TLS
prop.setProperty("mail.imap.starttls.enable", "false");

// Use SSL
prop.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
prop.setProperty("mail.imap.socketFactory.fallback", "false");

// Use port 993
prop.setProperty("mail.imap.port", "993");
prop.setProperty("mail.imap.socketFactory.port", "993");

prop.setProperty("mail.imaps.class", "com.sun.mail.imap.IMAPSSLStore");

Session session = Session.getDefaultInstance(prop);
session.setDebug(true);
Store store = session.getStore("imap");
store.connect(host, user, password);

At this point, I get javax.mail.AuthenticationFailedException: Command received in Invalid state. The user parameter is a combination of "domain\user-login\mail-box-alias". I use this because I saw it in this way in a how-to guide. I already installed a valid SSL certificade in my Weblogic Server.

I don't know where the error is.

With debug I get this:

DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]

OK The Microsoft Exchange IMAP4 service is ready.

A0 CAPABILITY

CAPABILITY IMAP4 IMAP4rev1 LOGINDISABLED STARTTLS CHILDREN IDLE NAMESPACE LITERAL+

A0 OK CAPABILITY completed.

A1 LOGIN "domain\user\mailbox-alias" password

A1 BAD Command received in Invalid state.

ERROR 2: javax.mail.AuthenticationFailedException: Command received in Invalid state.

3

3 Answers

2
votes

If you're using port 993, you want imaps instead of imap as your protocol:

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

On port 143, the connection is plaintext. You can then optionally start a TLS encryption layer by issuing the STARTTLS IMAP command. (That's what's controlled by the mail.imap.starttls.enable property.)

On port 993, the initial connection must be an encrypted SSL connection. This SSL handshake has to be done before any text is sent or received from the server.

0
votes

Make sure Exchange 2010 is set up to allow user/pwd based logins.

0
votes

Finally I resolved this issue. It was a problem of javamail version, it was too old for Exchange 2010. I upgrated to 1.4 version and worked. Until now I was using Javamail 1.1.3.