使用 IMAP 从 GMail 获取邮件到 Java 应用程序

我想使用 JavaMailIMAP从 Java 应用程序访问 Gmail 中的消息。为什么我要做 一个 href = “ https://docs.oracle.com/javase/7/docs/api/java/net/SocketTimeoutException.html”rel = “ nofollow noReferrer”> SocketTimeoutException

这是我的代码:

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

我已经设置了超时值,这样就不会“永远”超时。此外,MyAuthenticator还有用户名和密码,这对 URL 来说似乎是多余的。还有别的方法来指定协议吗?(我在 JavaDoc 的 IMAP中没有看到它。)

105732 次浏览

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

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%40gmail.com@imap.gmail.com/INBOX.

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

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.

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

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.

URLName server = new URLName("imaps://<gmail-user-name>:<gmail-pass>@imap.gmail.com/INBOX");

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"

You need to have JSSE installed to use SSL with Java

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)

Here is what worked for my team and I, given a classic account nickname@gmail.com and a 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 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 :