commons-email,TLS加密后报错

Posted xaioqidesimihuanyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了commons-email,TLS加密后报错相关的知识,希望对你有一定的参考价值。

commons-email-1.4.jar
javax.mail-1.5.2.jar

public class mail {
    public static void main(String[] args)  {
        MultiPartEmail  mail = new MultiPartEmail ();
        mail.setHostName("192.168.10.30");
        mail.setAuthentication("[email protected]", "fhAS");
        mail.setCharset("UTF-8");
        try {
            mail.setFrom("[email protected]", "红天桃客户服务");
            mail.addTo("[email protected]","Chrome");
            mail.setSubject("待办事务提醒");
            mail.setMsg("A" + "有一条新记录" + "Z" + "等待审核");
            mail.setStartTLSEnabled(true);
            mail.setSmtpPort(587);
            mail.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }

    }
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "15728640"
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    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
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2155)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:752)
    at javax.mail.Service.connect(Service.java:366)
    at javax.mail.Service.connect(Service.java:246)
    at EmailTest.main(EmailTest.java:42)
Caused by: 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
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1884)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1439)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:209)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:878)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:814)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:626)
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:553)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2150)
    ... 4 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1421)
    ... 14 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
    ... 20 more

JDK1.7无法运行,报错为PKIX:unable to find valid certification path to requested target,更换1.8后正常运行。

更换后报错
java.lang.RuntimeException: java.io.IOException: invalid constant type: 15,需单独指定javassist 包版本,升级为3.20.0-GA问题解决。

推荐另一种方法(需要在spring中的相关jar)
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    /**
     * 初始化邮件发送数据
     *
     * @param host     服务器
     * @param username 发送人
     * @param passwd   发送人密码
     */
    public void initData(String host, String username, String passwd)
    {
        //创建邮件发送服务器
        mailSender.setHost(host);
        mailSender.setPort(587);
        mailSender.setUsername(username);
        mailSender.setPassword(passwd);
        //加认证机制
        Properties javaMailProperties = new Properties();
        javaMailProperties.put("mail.smtp.auth", true);
        javaMailProperties.put("mail.smtp.starttls.enable", true);
        javaMailProperties.put("mail.smtp.timeout", 5000);
        javaMailProperties.put("mail.smtp.ssl.trust", "*");
        mailSender.setJavaMailProperties(javaMailProperties);
        System.out.println("初始化邮件发送信息完成");
    }

    /**
     * 发送普通文本
     *
     * @param email   对方邮箱地址
     * @param subject 主题
     * @param text    邮件内容
     */
    public void simpleMailSend(String email, String subject, String text)
    {
        //创建邮件内容
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(mailSender.getUsername());
        message.setTo(email);
        message.setSubject(subject);
        message.setText(text);
        //发送邮件
        mailSender.send(message);
        System.out.println("发送成功");
    }


    @Test
    public void testSpringEmail()
    {
        initData("192.168.10.30","[email protected]","fh");

        simpleMailSend("[email protected]","A","123");
    }





以上是关于commons-email,TLS加密后报错的主要内容,如果未能解决你的问题,请参考以下文章

代码提交svn错误后报错,总是提交不了。后来改了svn地址,依旧报错

odoo首页添加自定义模块,修改代码后报错,请问各路大神啥情况

Sublime Text 升级后报错‘kill_previous‘问题解决

git 拖下laravel 代码后报错 Warning: require(D:WWWlaravelootstrap/../vendor/autoload.php

发版后报错指数级上升!代码出bug了怎么办?怎样才能少出bug?

Pycharm(Django)运行后报错,EOFError: marshal data too short