Search code examples
javassljakarta-mailimap

Java imap connect to store with and without ssl


i'm trying to connect to imap store based on users server. For example, i have multiple servers like 143 with ssl, and 993 without ssl. When i try to connect to 143 ssl server it says plain connection. Is there any way to connect to store like that?

And is there any other IMAP other than javamail in java?

Some code below->

Properties props = System.getProperties();
String imapProtocol = server.getImapSsl() != null && server.getImapSsl() == true ? "imaps" : "imap";
props.put("mail.store.protocol", imapProtocol);
props.put("mail." + imapProtocol + ".port", server.getImapPort());
props.put("mail." + imapProtocol + ".host", server.getImapHost());

javax.mail.Session mailSession = javax.mail.Session.getInstance(props, null);
Store store = mailSession.getStore();
store.connect(server.getImapHost(), server.getImapPort(), username, password);

UPDATE (DEBUG)->

DEBUG IMAP: enable STARTTLS
DEBUG IMAP: closeFoldersOnStoreFailure
DEBUG IMAP: trying to connect to host "imap.itheme.mn", port 143, isSSL false
* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE NAMESPACE STARTTLS AUTH=PLAIN AUTH=LOGIN] Dovecot ready.
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: AUTH: LOGIN
DEBUG IMAP: protocolConnect login, host=imap.somemail.mn, [email protected], password=<non-null>
A0 STARTTLS
A0 OK Begin TLS negotiation now.
A1 CAPABILITY
* CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE NAMESPACE AUTH=PLAIN AUTH=LOGIN
A1 OK Pre-login capabilities listed, post-login capabilities have more.
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: AUTH: LOGIN
DEBUG IMAP: AUTHENTICATE LOGIN command trace suppressed
DEBUG IMAP: AUTHENTICATE LOGIN command result: A2 NO [AUTHENTICATIONFAILED] Authentication failed.

Solution

  • You're got the port numbers backwards. 143 is plain text.