使用imaps协议连接邮件服务,证书不符合算法

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

我正在尝试使用 imaps 协议访问邮件服务 (zimbra)。 我正在使用 javaMail jar 版本 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 */

打印异常:

 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)

首先,我从网页上获得了证书(.crt 文件),如下图所示。其次,我使用 keytool 命令

导入了证书
keytool  -importcert -file company.net.crt -keystore company.net -alias "company.net" -storepass changeit

picture 1 picture 2

伙计们,我错过了什么?

将您的代码更改为:

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 */

注意 use Session.getInstance 的关键变化。

希望您也按照 Notes for use of SSL with JavaMail.

中的描述设置信任库 属性

如果还是不行,post JavaMail debug output。您可能还想启用一些 SSL 调试属性,如上文所述。

哦,JavaMail 1.4.7 已经很老了。如果可能,您应该升级到最新版本 JavaMail 1.5.6