使用javax.mail通过SSL连接使用Imap接收电子邮件 [英] Receiving email using Imap through SSL connection using javax.mail
问题描述
我想使用imap槽式安全连接接收电子邮件.我使用javax.mail api实现了它.但是有不同的服务器配置.我发现
I want to receive emails using imap trough secure connection. I implemented it using using javax.mail api. But there are different server configurations. As I found
1) store = session.getStore(imaps);
store.connect(imap.gmail.com, username, password)
将'isSSL'设置为true并使用端口993(这是用于连接javax.mail的安全端口).以下配置还证明了通过993端口的安全连接.
Which make 'isSSL' true and use port 993 which is secure port to connect in javax.mail. Following configuration also prove secure connection through 993 port.
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);
这两种方法都可以正常工作.您能告诉我这两者之间的区别是什么,以及通过安全连接接收消息的正确方法是什么.我进一步发现;"mail.imap.ssl.enable"和"mail.imap.starttls.enable".你能告诉我我是否也需要这两个.
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.
推荐答案
设置各种socketFactory属性.很久很久以前,JavaMail尚未内置对SSL连接的支持,因此有必要将这些属性设置为使用SSL.多年以来情况并非如此.删除这些属性并简化您的代码.在当前版本的JavaMail中启用SSL支持的最简单方法是将属性"mail.smtp.ssl.enable"设置为"true".(将"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();
这篇关于使用javax.mail通过SSL连接使用Imap接收电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!