Java and Microsoft Exchange 2007

Hi.

I'm trying to connect to microsoft Exchange 2007 by using javamail on IMAP.

I'm using this code:

import java.util.Properties;

import javax.mail.Session;

import javax.mail.Store;

import javax.mail.event.ConnectionEvent;

publicclass MailHandler{

/**

* @param args

*/

publicstaticvoid main(String[] args)throws Exception{

//Get system properties

Properties props = System.getProperties();

//Specify the desired SMTP server

props.put("mail.imap.host","XXX.XXX.XX.XXX");

//props.put("mail.imap.host", "mail.eng.it");

props.put("mail.imap.starttls.enable","true");

props.put("mail.debug","true");

Session ses = Session.getInstance(props);

Store store = ses.getStore("imap");

//store.addConnectionListener(new ConnectionListener("administrator:MAIL_STORE"));

String imapHost = ses.getProperty("mail.imap.host");

//store.connect(imapHost,143, "administrator", "admin");

store.connect(imapHost,"administrator","admin");

//store.connect(imapHost, "angimmed", "janine1974");

}

}

class ConnectionListenerimplements javax.mail.event.ConnectionListener{

public ConnectionListener(String service){

super();

_service = service;

}

publicvoid closed(ConnectionEvent event){

long uptime = (System.currentTimeMillis() - _startTime) / 1000;

System.out.println(

"The " + _service +" service has been closed after " +

uptime +"secs of uptime.");

}

publicvoid disconnected(ConnectionEvent event){

long uptime = (System.currentTimeMillis() - _startTime) / 1000;

System.out.println(

"The " + _service +" service has been disconnected after " +

uptime +"secs of uptime.");

}

publicvoid opened(ConnectionEvent event){

_startTime = System.currentTimeMillis();

System.out.println("The " + _service +" service has been opened");

}

private String _service;

privatelong _startTime;

}

If i use this code by using other mail server (Cyrus+Postfix) all works pretty good; if i use it on Microsoft Exchange i have an error. The error is:

* OK Microsoft Exchange Server 2007 IMAP4 service ready

A0 CAPABILITY

* CAPABILITY IMAP4 IMAP4rev1 LOGINDISABLED STARTTLS IDLE NAMESPACE LITERAL+

A0 OK CAPABILITY completed.

A1 LOGIN administrator admin

A1 BAD Command received in Invalid state.

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

at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:330)

at javax.mail.Service.connect(Service.java:233)

at javax.mail.Service.connect(Service.java:134)

at mail.MailHandler.main(MailHandler.java:29)

Exception in thread "main"

My environmet is this one:

JVM: 1.4.2_12

Javamail version: 1.3.1

Can anyone tell me if this is a Microsoft Exchange 2007 issue or is it my code problem.

Thanx to all,

Angelo.

[4970 byte] By [craig1980a] at [2007-11-27 9:44:10]
# 1

You're using a pretty old version of JavaMail. If possible, you should upgrade

to a newer version that includes STARTTLS support, which your server seems

to require.

Alternatively, your server might accept connections on the SSL port (which, like

STARTTLS, ensures a secure connection over which to send your password),

but configuring that is more difficult with older versions of JavaMail.

bshannona at 2007-7-12 23:50:38 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Hello.Thanx for your reply.Sometime the simplest solutions are not considered. By changin form version 1.3.1 to version 1.4 seems to work.Thank again.
craig1980a at 2007-7-12 23:50:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...