使用 IMAP 从 GMail 获取邮件到 Java 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/61176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting mail from GMail into Java application using IMAP
提问by Dave
I want to access messages in Gmail from a Java application using JavaMailand IMAP. Why am I getting a SocketTimeoutException?
我想使用JavaMail和IMAP从 Java 应用程序访问 Gmail 中的邮件。为什么我会收到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, MyAuthenticatoralso 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.)
我已经设置了超时值,这样就不会“永远”超时。此外,MyAuthenticator也有用户名和密码,这对于 URL 来说似乎是多余的。有没有另一种方法来指定协议?(我没有在IMAP的 JavaDoc 中看到它。)
回答by Brian Matthews
You have to connect to GMail using SSL only. Setting the following properties will force that for you.
您必须仅使用 SSL 连接到 GMail。设置以下属性将为您强制执行。
props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.imap.socketFactory.fallback", "false");
回答by Chris Jester-Young
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
.
在 JavaMail 中,您可以将其imaps
用作 URL 方案以通过 SSL 使用 IMAP。(有关SSLNOTES.txt
更多详细信息,请参阅JavaMail 分发版。)例如,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!
同样,用于smtps
通过 Gmail 发送电子邮件。例如,smtps://username%[email protected]/
。再次阅读SSLNOTES.txt
以了解更多详细信息。希望能帮助到你!
回答by Dave
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.
使用 imaps 是一个很好的建议。提供的答案都没有对我有用,所以我搜索了更多并找到了一些有用的东西。这是我的代码现在的样子。
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.
这很好,因为它从图片中删除了冗余的 Authenticator。我很高兴这奏效了,因为 SSLNOTES.txt 使我头晕目眩。
回答by Adisesha
Check http://g4j.sourceforge.net/. There is a minimal gmail client built using this API.
检查http://g4j.sourceforge.net/。使用此 API 构建了一个最小的 gmail 客户端。
回答by Zach Scrivena
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 GmailAssistanton SourceForge.
如果您想要更多关于在 Gmail 中使用 JavaMail 的示例代码(例如将 Gmail 标签转换为 IMAP 文件夹名称,或使用 IMAP IDLE),请查看我在SourceForge 上的程序GmailAssistant。
回答by Zach Scrivena
URLName server = new URLName("imaps://<gmail-user-name>:<gmail-pass>@imap.gmail.com/INBOX");
回答by Alex Cheng
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"
"mail.imaps.host" : "imap.gmail.com"
"mail.store.protocol" : "imaps"
"mail.imaps.port" : "993"
回答by hd1
You need to have JSSE installed to use SSL with Java
您需要安装 JSSE 才能在 Java 中使用 SSL
回答by WhyNotHugo
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)
请注意它是“imaps”,而不是“imap”,因为您使用的协议是 imaps (IMAP + SSL)
回答by lboix
Here is what worked for my team and I, given a classic account [email protected] and a business account [email protected] :
以下是对我和我的团队有用的方法,给定一个经典帐户[email protected] 和一个企业帐户[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.
在经典案例中使用 USERNAME = "nickname",在企业帐户案例中使用 USERNAME = "[email protected]"。
In the classic case, and if you use an old JavaMail dependency, don't forget to lower the account security here : https://www.google.com/settings/security/lesssecureapps
在经典情况下,如果您使用旧的 JavaMail 依赖项,请不要忘记在此处降低帐户安全性:https: //www.google.com/settings/security/lesssecureapps
In both cases check in GMail Settings => Forwarding POP / IMAPif IMAP is enabled for the account.
在这两种情况下,如果帐户启用了IMAP,请检查 GMail设置 => 转发 POP / IMAP。
Hope it helps!
希望能帮助到你!
To go further :
更进一步: