简体   繁体   English

使用 javax.mail 通过 SSL 连接使用 Imap 接收电子邮件

[英]Receiving email using Imap through SSL connection using javax.mail

I want to receive emails using imap trough secure connection.我想通过安全连接使用 imap 接收电子邮件。 I implemented it using using javax.mail api.我使用 javax.mail api 实现了它。 But there are different server configurations.但是有不同的服务器配置。 As I found正如我发现的

1)  store = session.getStore(imaps);
    store.connect(imap.gmail.com, username, password)

Which make 'isSSL' true and use port 993 which is secure port to connect in javax.mail.这使得“isSSL”为真,并使用端口 993,这是在 javax.mail 中连接的安全端口。 Following configuration also prove secure connection through 993 port.以下配置也证明了通过 993 端口的安全连接。

 2) properties.put("mail.imap.host", imap.gmail.com);
    properties.put("mail.imap.port", "993");
    Properties.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    properties.setProperty("mail.imap.socketFactory.fallback","false");
    Properties.setProperty("mail.imap.socketFactory.port", 993);

These two methods work fine.这两种方法效果很好。 Can you please tell me what is different between these two and what is the correct way to receive messages through secure connection.你能告诉我这两者之间有什么不同,以及通过安全连接接收消息的正确方法是什么。 Futher I found;我进一步发现; "mail.imap.ssl.enable" and "mail.imap.starttls.enable. Can you tell me whether i needed these two also. “mail.imap.ssl.enable”和“mail.imap.starttls.enable。你能告诉我我是否也需要这两个。

Setting various socketFactory properties.设置各种 socketFactory 属性。 Long, long ago JavaMail didn't have built in support for SSL connections, so it was necessary to set these properties to use SSL.很久很久以前,JavaMail 没有内置对 SSL 连接的支持,因此有必要设置这些属性以使用 SSL。 This hasn't been the case for years;多年来,情况并非如此。 remove these properties and simplify your code.删除这些属性并简化您的代码。 The easiest way to enable SSL support in current versions of JavaMail is to set the property "mail.smtp.ssl.enable" to "true".在当前版本的 JavaMail 中启用 SSL 支持的最简单方法是将属性“mail.smtp.ssl.enable”设置为“true”。 (Replace "smtp" with "imap" or "pop3" as appropriate.) https://javaee.github.io/javamail/FAQ#commonmistakes (根据需要将“smtp”替换为“imap”或“pop3”。) https://javaee.github.io/javamail/FAQ#commonmistakes

String host = "mail.example.com";
String username = "email@example.com";
String password = "mysecretpassword";

Properties props = new Properties();
props.setProperty("mail.imap.ssl.enable", "true");

Session session = javax.mail.Session.getInstance(props);
Store store = session.getStore("imap");
store.connect(host, username, password);

Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();


inbox.close(false);
store.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM