<p>I want to access messages in Gmail from a Java application using <a href="http://www.ing.iac.es/~docs/external/java/javamail/javadocs/index.html" rel="nofollow noreferrer">JavaMail</a> and <a href="https://en.wikipedia.org/wiki/Internet_Message_Access_Protocol" rel="nofollow noreferrer">IMAP</a>. Why am I getting a <em><a href="https://docs.oracle.com/javase/7/docs/api/java/net/SocketTimeoutException.html" rel="nofollow noreferrer">SocketTimeoutException</a></em> ?</p>

<p>Here is my code:</p>

<pre><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);
}
</code></pre>

<p>I have set the timeout values so that it wouldn't take "forever" to timeout. Also, <em>MyAuthenticator</em> 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 <a href="https://en.wikipedia.org/wiki/Internet_Message_Access_Protocol" rel="nofollow noreferrer">IMAP</a>.)</p>

[{'answer_id': 61179, 'author': 'Brian Matthews', 'author_id': 1969, 'author_profile': 'https://Stackoverflow.com/users/1969', 'pm_score': 2, 'selected': False, 'text': '<p>You have to connect to GMail using SSL only. Setting the following properties will force that for you. </p>\n\n<pre>\nprops.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");\nprops.setProperty("mail.imap.socketFactory.fallback", "false");\n</pre>\n'}, {'answer_id': 61185, 'author': 'C. K. Young', 'author_id': 13, 'author_profile': 'https://Stackoverflow.com/users/13', 'pm_score': 3, 'selected': False, 'text': '<p>In JavaMail, you can use <code>imaps</code> as the URL scheme to use IMAP over SSL. (See <code>SSLNOTES.txt</code> in your JavaMail distribution for more details.) For example, <code>imaps://username%40gmail.com@imap.gmail.com/INBOX</code>.</p>\n\n<p>Similarly, use <code>smtps</code> to send emails via Gmail. e.g., <code>smtps://username%40gmail.com@smtp.gmail.com/</code>. Again, read <code>SSLNOTES.txt</code> for more details. Hope it helps!</p>\n'}, {'answer_id': 61469, 'author': 'Dave', 'author_id': 2512222, 'author_profile': 'https://Stackoverflow.com/users/2512222', 'pm_score': 6, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>Properties props = System.getProperties();\nprops.setProperty("mail.store.protocol", "imaps");\ntry {\n  Session session = Session.getDefaultInstance(props, null);\n  Store store = session.getStore("imaps");\n  store.connect("imap.gmail.com", "&lt;username&gt;@gmail.com", "&lt;password&gt;");\n  ...\n} catch (NoSuchProviderException e) {\n  e.printStackTrace();\n  System.exit(1);\n} catch (MessagingException e) {\n  e.printStackTrace();\n  System.exit(2);\n}\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 69724, 'author': 'Adisesha', 'author_id': 11092, 'author_profile': 'https://Stackoverflow.com/users/11092', 'pm_score': 1, 'selected': False, 'text': '<p>Check <a href="http://g4j.sourceforge.net/" rel="nofollow noreferrer">http://g4j.sourceforge.net/</a>. There is a minimal gmail client built using this API.</p>\n'}, {'answer_id': 112173, 'author': 'Zach Scrivena', 'author_id': 20029, 'author_profile': 'https://Stackoverflow.com/users/20029', 'pm_score': 2, 'selected': False, 'text': '<p>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 <a href="http://gmailassistant.sourceforge.net/" rel="nofollow noreferrer">GmailAssistant</a> on <a href="http://www.sourceforge.net/" rel="nofollow noreferrer">SourceForge</a>.</p>\n'}, {'answer_id': 260650, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<pre><code>URLName server = new URLName("imaps://&lt;gmail-user-name&gt;:&lt;gmail-pass&gt;@imap.gmail.com/INBOX");\n</code></pre>\n'}, {'answer_id': 2385657, 'author': 'Fu Cheng', 'author_id': 221847, 'author_profile': 'https://Stackoverflow.com/users/221847', 'pm_score': 1, 'selected': False, 'text': '<p>I used following properties to get the store and It works well.</p>\n\n<p><code>"mail.imaps.host" : "imap.gmail.com" <br>\n"mail.store.protocol" : "imaps" <br>\n"mail.imaps.port" : "993"\n</code></p>\n'}, {'answer_id': 3952463, 'author': 'hd1', 'author_id': 783412, 'author_profile': 'https://Stackoverflow.com/users/783412', 'pm_score': 0, 'selected': False, 'text': '<p>You need to have JSSE installed to use SSL with Java</p>\n'}, {'answer_id': 9888150, 'author': 'WhyNotHugo', 'author_id': 107510, 'author_profile': 'https://Stackoverflow.com/users/107510', 'pm_score': 3, 'selected': False, 'text': '<p>You need to use the following properties for imaps:</p>\n\n<pre><code>props.setProperty("mail.imaps.host", "imap.gmail.com");\nprops.setProperty("mail.imaps.port", "993");\nprops.setProperty("mail.imaps.connectiontimeout", "5000");\nprops.setProperty("mail.imaps.timeout", "5000");\n</code></pre>\n\n<p>Notices it\'s "imaps", not "imap", since the protocol you\'re using is imaps (IMAP + SSL)</p>\n'}, {'answer_id': 31214560, 'author': 'lboix', 'author_id': 2920131, 'author_profile': 'https://Stackoverflow.com/users/2920131', 'pm_score': 2, 'selected': False, 'text': '<p>Here is what worked for my team and I, given a classic account nickname@gmail.com and a business account employee@business.com :</p>\n<pre><code>            final Properties properties = new Properties();\n            properties.put(&quot;mail.imap.ssl.enable&quot;, &quot;true&quot;);\n\n            imapSession = Session.getInstance(properties, null);\n            imapSession.setDebug(false);\n            imapStore = imapSession.getStore(&quot;imap&quot;);\n\n            imapStore.connect(&quot;imap.gmail.com&quot;, USERNAME, &quot;password&quot;);\n</code></pre>\n<p>with USERNAME = &quot;nickname&quot; in the classic case, and USERNAME = &quot;employee@business.com&quot; in the business account case.</p>\n<p>In the classic case, don\'t forget to lower the account security here : <a href="https://www.google.com/settings/security/lesssecureapps" rel="nofollow noreferrer">https://www.google.com/settings/security/lesssecureapps</a></p>\n<p>In both cases check in GMail <em>Settings =&gt; Forwarding POP / IMAP</em> if IMAP is enabled for the account.</p>\n<p>Hope it helps!</p>\n<p>To go further :</p>\n<ul>\n<li><a href="http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail" rel="nofollow noreferrer">http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail</a></li>\n<li><a href="https://support.google.com/mail/accounts/answer/78754" rel="nofollow noreferrer">https://support.google.com/mail/accounts/answer/78754</a></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/61176', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2512222/']