Getting mail from GMail into Java application using IMAP

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2008-09-14T07:11:31Z Indexed on 2010/03/13 2:57 UTC
Read the original article Hit count: 522

Filed under:
|
|

I want to access messages in GMail from a Java application using JavaMail and IMAP. Why am I getting a SocketTimeoutException?

Here is my code:

Properties props = System.getProperties();
props.setProperty("mail.imap.host", "imap.gmail.com");
props.setProperty("mail.imap.port", "993");
props.setProperty("mail.imap.connectiontimeout", "5000");
props.setProperty("mail.imap.timeout", "5000");

try {
  Session session = Session.getDefaultInstance(props, new MyAuthenticator());
  URLName urlName = new URLName("imap://[email protected]:[email protected]");
  Store store = session.getStore(urlName);
  if (!store.isConnected()) {
    store.connect();
  }
} catch (NoSuchProviderException e) {
  e.printStackTrace();
  System.exit(1);
} catch (MessagingException e) {
  e.printStackTrace();
  System.exit(2);
}

I set the timeout values so that it wouldn't take "forever" to timeout. Also, MyAuthenticator also has the username and password, which seems redundant with the URL. Is there another way to specify the protocol? (I didn't see it in the JavaDoc for IMAP.)

© Stack Overflow or respective owner

Related posts about java

Related posts about gmail