我正在尝试使用端口 25 上的自签名证书连接到 SMTP 服务器(James 3),并打开 STARTTLS。
我已启用 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();`