Getting mail from GMail into Java application using IMAP
Asked Answered
C

10

76

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 have 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.)

Christianity answered 14/9, 2008 at 7:11 Comment(3)
If you want access GMail threads using JavaMail you might be interested in code.google.com/p/java-gmail-imapSabba
Do any of these IMAP-based solutions work anymore without the need to "Allow less secure apps"?Ravo
Related: security.stackexchange.com/questions/66025/…Ravo
C
71

Using imaps was a great suggestion. Neither of the answers provided just worked for me, so I googled some more and found something that worked. Here's how my code looks now.

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 nice because it takes the redundant Authenticator out of the picture. I'm glad this worked because the SSLNOTES.txt make my head spin.

Christianity answered 14/9, 2008 at 17:8 Comment(0)
I
9

You need to use the following properties for imaps:

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");

Notices it's "imaps", not "imap", since the protocol you're using is imaps (IMAP + SSL)

Interment answered 27/3, 2012 at 10:50 Comment(0)
P
6

In JavaMail, you can use imaps as the URL scheme to use IMAP over SSL. (See SSLNOTES.txt in your JavaMail distribution for more details.) For example, imaps://username%[email protected]/INBOX.

Similarly, use smtps to send emails via Gmail. e.g., smtps://username%[email protected]/. Again, read SSLNOTES.txt for more details. Hope it helps!

Parr answered 14/9, 2008 at 7:30 Comment(0)
S
3

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

props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.imap.socketFactory.fallback", "false");
Siena answered 14/9, 2008 at 7:22 Comment(2)
I don't recommend this approach in general, because it overrides all imap connections to be imaps. Sometimes choice is a good thing, especially if the program is to be used for connecting to just Gmail. However, if you think plain IMAP is outright wrong, then this approach is acceptable. :-)Parr
...used for connecting to more than just Gmail, I meant. :-PParr
U
3

Here is what worked for my team and I, given a classic account [email protected] and a business account [email protected] :

            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 = "[email protected]" in the business account case.

In the classic case, don't forget to lower the account security here : https://www.google.com/settings/security/lesssecureapps

In both cases check in GMail Settings => Forwarding POP / IMAP if IMAP is enabled for the account.

Hope it helps!

To go further :

Unparalleled answered 3/7, 2015 at 21:49 Comment(1)
What do you mean by "old JavaMail dependency"? I don't think this has anything to do with the JavaMail version, it's that you're connecting with IMAPS instead of OAUTH2.Ravo
A
2

If you'd like more sample code on using JavaMail with Gmail (e.g. converting Gmail labels to IMAP folder names, or using IMAP IDLE), do check out my program GmailAssistant on SourceForge.

Afternoon answered 21/9, 2008 at 21:8 Comment(0)
T
1

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

Topper answered 16/9, 2008 at 6:9 Comment(0)
V
1

I used following properties to get the store and It works well.

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

Venditti answered 5/3, 2010 at 9:27 Comment(0)
D
0
URLName server = new URLName("imaps://<gmail-user-name>:<gmail-pass>@imap.gmail.com/INBOX");
Daegal answered 4/11, 2008 at 2:37 Comment(0)
M
0

You need to have JSSE installed to use SSL with Java

Mullinax answered 17/10, 2010 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.