java 不验证证书,不带身份验证信任证书的JavaMail SSL,无论如何

I have a local mail server (hMailServer) with SSL (port 465) and a self-signed certificate.

Domain is "foobar.com"

I have setup my Properties to enable ssl, disable auth, and trust any host

props.put("mail.smtp.auth", "false");

props.put("mail.smtp.ssl.enable", "true");

props.put("mail.smtp.ssl.trust", "*");

If I send the message through the static call to Transport.send()

The email gets delivered.

If I try to get a transport instance from the session then I get

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

How does the static call avoids the SSLHandshakeException?

Here's my tester code:

public static void main(String[] args) throws Exception {

Properties props = new Properties();

props.put("mail.smtp.host", "127.0.0.1");

props.put("mail.debug", "false");

props.put("mail.smtp.port", "465");

props.put("mail.smtp.timeout", "60000");

props.put("mail.smtp.auth", "false");

props.put("mail.smtp.sendpartial", "true");

props.put("mail.smtp.ssl.enable", "true");

props.put("mail.smtp.ssl.trust", "*");

Session session = Session.getInstance(props);

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress("mrFoo@foobar.com"));

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("you@foobar.com"));

message.setSubject("Just a Test " + new Date());

message.setText("Hello World");

//Comment and uncomment to test

Transport.send(message, message.getAllRecipients());

//Transport t = session.getTransport("smtps");

//t.connect();

//t.sendMessage(message, message.getAllRecipients());

//t.close();

}

This is a local system hidden from the outside, so I am not worried about man in the middle attack generating their own certificates to bypass the SSL Handshake...

解决方案

You asked for an "smtps" transport. You set the properties for the "smtp" transport. Since you've set the "mail.smtp.ssl.enable" property to "true", you can just ask for an "smtp" transport and it will use SSL.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值