[英]Getting mail from GMail into Java application using IMAP
I want to access messages in Gmail from a Java application using JavaMail and IMAP .我想使用JavaMail和IMAP从 Java 应用程序访问 Gmail 中的消息。 Why am I getting a SocketTimeoutException ?
为什么我会收到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 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.
此外, MyAuthenticator也有用户名和密码,这对于 URL 来说似乎是多余的。 Is there another way to specify the protocol?
还有另一种指定协议的方法吗? (I didn't see it in the JavaDoc for IMAP .)
(我没有在IMAP的 JavaDoc 中看到它。)
Using imaps was a great suggestion.使用 imaps 是一个很好的建议。 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.这很好,因为它消除了多余的 Authenticator。 I'm glad this worked because the SSLNOTES.txt make my head spin.
我很高兴这能奏效,因为 SSLNOTES.txt 让我头晕目眩。
You need to use the following properties for imaps:您需要为 imap 使用以下属性:
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)注意它是“imap”,而不是“imap”,因为您使用的协议是 imaps (IMAP + SSL)
In JavaMail, you can use imaps
as the URL scheme to use IMAP over SSL.在 JavaMail 中,您可以将
imaps
用作 URL 方案,以在 SSL 上使用 IMAP。 (See SSLNOTES.txt
in your JavaMail distribution for more details.) For example, imaps://username%40gmail.com@imap.gmail.com/INBOX
. (有关更多详细信息,请参阅 JavaMail 分发中的
SSLNOTES.txt
。)例如, imaps://username%40gmail.com@imap.gmail.com/INBOX
。
Similarly, use smtps
to send emails via Gmail.同样,使用
smtps
通过 Gmail 发送电子邮件。 eg, smtps://username%40gmail.com@smtp.gmail.com/
.例如,
smtps://username%40gmail.com@smtp.gmail.com/
。 Again, read SSLNOTES.txt
for more details.同样,阅读
SSLNOTES.txt
以了解更多详细信息。 Hope it helps!希望能帮助到你!
Here is what worked for my team and I, given a classic account nickname@gmail.com and a business account employee@business.com:这是我和我的团队的工作,给定一个经典帐户昵称@gmail.com 和一个企业帐户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.在经典案例中使用 USERNAME = "nickname",在企业帐户案例中使用 USERNAME = "employee@business.com"。
In the classic case, don't forget to lower the account security here: https://www.google.com/settings/security/lesssecureapps在经典案例中,不要忘记在此处降低帐户安全性: https://www.google.com/settings/security/lesssecureapps
In both cases check in GMail Settings => Forwarding POP / IMAP if IMAP is enabled for the account.在这两种情况下,检查 GMail设置 => 如果为帐户启用了 IMAP,则转发 POP / IMAP 。
Hope it helps!希望能帮助到你!
To go further:到 go 进一步:
You have to connect to GMail using SSL only.您必须仅使用 SSL 连接到 GMail。 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");
If you'd like more sample code on using JavaMail with Gmail (eg converting Gmail labels to IMAP folder names, or using IMAP IDLE), do check out my program GmailAssistant on SourceForge .如果您想要更多关于将 JavaMail 与 Gmail 一起使用的示例代码(例如,将 Gmail 标签转换为 IMAP 文件夹名称,或使用 IMAP IDLE),请查看我在SourceForge 上的GmailAssistant程序。
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"
Check http://g4j.sourceforge.net/ .检查http://g4j.sourceforge.net/ 。 There is a minimal gmail client built using this API.
使用此 API 构建了一个最小的 gmail 客户端。
URLName server = new URLName("imaps://<gmail-user-name>:<gmail-pass>@imap.gmail.com/INBOX");
You need to have JSSE installed to use SSL with Java您需要安装 JSSE 才能使用 SSL 和 Java
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.