Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

how to read inbox using javamail from microsoft exchange server

819306Jan 27 2011 — edited Feb 2 2011
Hi,

I want to read mails from microsoft exchange server using javamail.I use the following code



String username = "ggg@fff.com";
String password = "sdasdas";

Properties props = System.getProperties();




Authenticator auth = new Authenticator()
{


public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username , password );
}
};
Session session = Session.getDefaultInstance(props, auth);


System.out.println(session);

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

System.out.println(store);
store.connect(host, username, password);
// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
// Get directory
Message message[] = folder.getMessages();
for (int i=0, n=message.length; i<n; i++) {
System.out.println(i + ": " + message.getFrom()[0]
+ "\t" + message[i].getSubject());
}
// Close connection
folder.close(false);
store.close();


But I got error

javax.mail.MessagingException: Connection timed out: connect;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:571)
at javax.mail.Service.connect(Service.java:288)
at javax.mail.Service.connect(Service.java:169)
at Shipmate.MyJavaMail.reaadMail(MyJavaMail.java:580)
at Shipmate.MyJavaMail.main(MyJavaMail.java:46)


I can access mails through Microsoft accesss office outlook in the configuration of that 1.it use connect to mymailbox using HTTP

2.In connection setting a Url is provided to connect to my proxyserver exchange.like https//sadsadsa.asdasds@asdsf.com


3.mutually authenticate the session with ssl and provide the principal name for proxy server

4.And also basic authentication is used for connecting my proxy server for exchange.


The above are the settings done in my microsoft outlook 2003 so that I can read the mail in that.


But I would like to read inbox using Javamail.How can I do that?

Thanks in Advance

Comments

Bill Shannon-Oracle Jan 27 2011
It sounds like you may be using a proxy server to access your mail server.
See this FAQ entry: http://www.oracle.com/technetwork/java/javamail/faq/index.html#proxy
819306 Jan 27 2011
hi

Thank you for your reply.
I can send mail to this exchange server without using any proxy.Just as normal javamail.But I couldn't read mails .I got timed out Exception.

Thanks in advance
Bill Shannon-Oracle Jan 27 2011
The JavaMail FAQ has tips for debugging connection problems, try those.

Are you sure your Exchange server has IMAP support enabled?
How you tried both plain text IMAP and IMAP over SSL?
819306 Jan 28 2011
couldn't find tips to avoid this exception and get connection.
Yes the server is imap enabled.
Bill Shannon-Oracle Jan 28 2011
Did you look here:
http://www.oracle.com/technetwork/java/javamail/faq/index.html#condebug
819306 Feb 2 2011
Hi

I got connection while telnet x.x.xc.xx 143 after disscusion with admin.And Again I try the program below then I got the error No login methods supported


String host="x.x.xc.xx";
final String username="sdsdsa@ss-ssmanagement.com";
final String password="88888888888";

Authenticator auth=new Authenticator()
{

protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}

};
Properties props=System.getProperties();
props.put("mail.imaps.timeout", "120000");
Session session = Session.getInstance(props,auth);
session.setDebug(true);
Store store = session.getStore("imap");


System.out.println("store------"+store);
store.connect(host, username, password);
System.out.println("Connected...");
Folder inbox = store.getDefaultFolder().getFolder("INBOX");
inbox.open(Folder.READ_ONLY);

Message[] msg = inbox.getMessages();

System.out.println(msg);

inbox.close(false);
store.close();

ANd I got the error as below

EBUG: trying to connect to host "x.x.xc.xx", port 143, isSSL false
* OK The Microsoft Exchange IMAP4 service is ready.
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 LOGINDISABLED IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG: protocolConnect login, host=x.x.xc.xx, user=sdsdsa@ss-ssmanagement.com, password=<non-null>
javax.mail.MessagingException: No login methods supported!;
nested exception is:
com.sun.mail.iap.ProtocolException: No login methods supported!
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:663)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at BsmMail.main(BsmMail.java:44)
Caused by: com.sun.mail.iap.ProtocolException: No login methods supported!
at com.sun.mail.imap.IMAPStore.login(IMAPStore.java:725)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:643)
... 3 more
Bill Shannon-Oracle Feb 2 2011
I suspect you need to connect over SSL. That is, use getStore("imaps").

(Note that you were setting the mail.imaps.timeout property even though
you were using the "imap" protocol. Probably not what you intended.
819306 Feb 2 2011 — edited on Feb 2 2011
Thank you for your quick reply,

I use Store store = session.getStore("imaps"); and I remove props.put("mail.imaps.timeout", "120000"); from program

String host="x.x.xc.xx";
final String username="sdsdsa@ss-ssmanagement.com";
final String password="88888888888";

Authenticator auth=new Authenticator()
{

protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}

};
Properties props=System.getProperties();

Session session = Session.getInstance(props,auth);
session.setDebug(true);
Store store = session.getStore("imaps");


System.out.println("store------"+store);
store.connect(host, username, password);
System.out.println("Connected...");
Folder inbox = store.getDefaultFolder().getFolder("INBOX");
inbox.open(Folder.READ_ONLY);

Message[] msg = inbox.getMessages();




then I got

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
store------imaps:
DEBUG: trying to connect to host "x.x.xc.xx", port 993, isSSL true
javax.mail.MessagingException: Unrecognized SSL message, plaintext connection?;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:665)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at BsmMail.main(BsmMail.java:44)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:507)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:113)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:110)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:632)
... 3 more

Edited by: Shanu on Feb 2, 2011 12:15 AM

Edited by: Shanu on Feb 2, 2011 12:17 AM
Bill Shannon-Oracle Feb 2 2011
I have no idea. Perhaps your server is configured to use a non-standard port
for IMAP over SSL? You'll need to talk to your mail server administrator.
1 - 9
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 2 2011
Added on Jan 27 2011
9 comments
15,209 views