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.

