javassl-certificatejakarta-mailimapzimbra

Connect to mail service with imaps protocol, Certificates does not conform to algorithm


I'm trying to access emails to a mail service (zimbra) with imaps protocol. I'm using javaMail jar version 1.4.7

Properties props = (Properties)System.getProperties().clone();
// SSL setting
props.put("mail.imaps.ssl.checkserveridentity", "false");
props.put("mail.imaps.ssl.trust", "*");
MailSSLSocketFactory socketFactory = new MailSSLSocketFactory();
socketFactory.setTrustAllHosts(true);
props.put("mail.imaps.ssl.socketFactory", socketFactory);
Store store = Session.getDefaultInstance(props).getStore("imaps");
store.connect(host, email, password); /* exception here */

print exception:

 javax.mail.MessagingException: java.security.cert.CertificateException: Certificates does not conform to algorithm constraints;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: Certificates does not conform to algorithm constraints
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:670)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)

First I've got the certificate (.crt file) from the web page as displayed in images below. Second, I've imported the certificate with keytool command

keytool  -importcert -file company.net.crt -keystore company.net -alias "company.net" -storepass changeit

picture 1 picture 2

What am I missing here guys?


Solution

  • Change your code to just this:

    props.put("mail.imaps.ssl.checkserveridentity", "false");
    props.put("mail.imaps.ssl.trust", "*");
    Store store = Session.getInstance(props).getStore("imaps");
    store.connect(host, email, password); /* exception here */
    

    Note the key change to use Session.getInstance.

    Hopefully you're also setting the trust store property as described in the Notes for use of SSL with JavaMail.

    If that still doesn't work, post the JavaMail debug output. You might also want to enable some of the SSL debugging properties as described in the above note.

    Oh, and JavaMail 1.4.7 is pretty old. You should upgrade to the latest release JavaMail 1.5.6 if possible.