Receive mail from GMail into a Java application using IMAP

I want to access GMail messages 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://MYUSERNAME@gmail.com:MYPASSWORD@imap.gmail.com"); 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 value so that it does not take "forever" until the timeout. In addition, MyAuthenticator also has a username and password, which seems superfluous with the URL. Is there any other way to specify a protocol? (I have not seen it in JavaDoc for IMAP.)

+71
java imap gmail
Sep 14 '08 at 7:11
source share
10 answers

Using imaps was a great suggestion. None of the answers provided to me just worked for me, so I searched googled even more and found something that worked. This is what my code looks like.

 Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); try { Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("imap.gmail.com", "<username>@gmail.com", "<password>"); ... } catch (NoSuchProviderException e) { e.printStackTrace(); System.exit(1); } catch (MessagingException e) { e.printStackTrace(); System.exit(2); } 

This is good because it takes the redundant Authenticator from the image. I'm glad it worked because SSLNOTES.txt makes my head spin.

+68
Sep 14 '08 at 17:08
source share

In JavaMail, you can use imaps as the URL scheme for using IMAP over SSL. (For more information, see SSLNOTES.txt in your JavaMail distribution.) For example, imaps://username%40gmail.com@imap.gmail.com/INBOX .

Similarly, use smtps to send emails via Gmail. e.g. smtps://username%40gmail.com@smtp.gmail.com/ . Read SSLNOTES.txt for more details. Hope this helps!

+6
Sep 14 '08 at 7:30
source share

For imaps, you must use the following properties:

 props.setProperty("mail.imaps.host", "imap.gmail.com"); props.setProperty("mail.imaps.port", "993"); props.setProperty("mail.imaps.connectiontimeout", "5000"); props.setProperty("mail.imaps.timeout", "5000"); 

Notes that these are "imaps" and not "imap", since the protocol you use is imaps (IMAP + SSL)

+6
Mar 27 '12 at 10:50
source share

You need to connect to GMail only using SSL. Setting the following properties will force this for you.

 props.setProperty ("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
 props.setProperty ("mail.imap.socketFactory.fallback", "false");
+3
Sep 14 '08 at 7:22
source share

If you want to use more sample code when using JavaMail with Gmail (for example, converting Gmail tags to IMAP folder names or using IMAP IDLE), check out my GmailAssistant program at SourceForge .

+2
Sep 21 '08 at 21:08
source share

Check http://g4j.sourceforge.net/ . There is a minimal gmail client built using this API.

+1
Sep 16 '08 at 6:09
source share

I used the following properties to get the repository and it works well.

"mail.imaps.host" : "imap.gmail.com"
"mail.store.protocol" : "imaps"
"mail.imaps.port" : "993"

+1
Mar 05
source share

Here is what worked for my team and I, given the classic nickname@gmail.com account and the business account employee@business.com:

  final Properties properties = new Properties(); properties.put("mail.imap.ssl.enable", "true"); imapSession = Session.getInstance(properties, null); imapSession.setDebug(false); imapStore = imapSession.getStore("imap"); imapStore.connect("imap.gmail.com", USERNAME, "password"); 

with USERNAME = "nickname" in the classic case, and USERNAME = "employee@business.com" in the business case.

In the classic case, if you are using the old JavaMail dependency, be sure to omit account protection here: https://www.google.com/settings/security/lesssecureapps

In both cases, check the settings GMail => Forward POP / IMAP if IMAP is enabled for the account.

Hope this helps!

Further:

+1
Jul 03 '15 at 21:49
source share
 URLName server = new URLName("imaps://<gmail-user-name>:<gmail-pass>@imap.gmail.com/INBOX"); 
0
Nov 04 '08 at 2:37
source share

You need to install JSSE to use SSL with Java

0
Oct 17 '10 at 7:53
source share



All Articles