1
我嘗試使用啓用STARTTLS的端口25上的自簽名證書連接到SMTP服務器(James 3)。Javamail STARTTLS使用Spring容器使用自簽名證書
我已經啓用JavaMail屬性來信任所有主機,但我仍然得到PKIX證書路徑驗證錯誤。我怎樣才能擺脫錯誤?
請參閱下面的代碼。
//Trust all hosts
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.auth.mechanisms", "PLAIN");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getInstance(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(ti.sutUserName));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(ti.sutEmailAddress));
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("This is message body");
Multipart multipart = new MimeMultipart();
log.info("Sending Message");
Transport transport = session.getTransport("smtp");
transport.connect(ti.sutSmtpAddress, ti.sutUserName, ti.sutPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();`
您使用的是什麼版本的JavaMail?嘗試擺脫所有顯式使用套接字工廠和相關屬性,並將屬性「mail.smtp.ssl.trust」設置爲「*」。如果這不起作用,請打開[JavaMail會話調試](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug),設置系統屬性「mail.socket.debug 「爲」true「,併發布調試輸出。 –