问题
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)
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);
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.
回答1:
Setting various socketFactory properties. Long, long ago JavaMail didn't have built in support for SSL connections, so it was necessary to set these properties to use 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". (Replace "smtp" with "imap" or "pop3" as appropriate.) 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();
来源:https://stackoverflow.com/questions/45446239/receiving-email-using-imap-through-ssl-connection-using-javax-mail