TestNG 电子邮件报告 - PKIX 路径构建异常

Posted

技术标签:

【中文标题】TestNG 电子邮件报告 - PKIX 路径构建异常【英文标题】:TestNG Email Report - PKIX path building exception 【发布时间】:2016-03-31 06:22:49 【问题描述】:

我突然在一个问题场景中运行 我在 selenium 中有 TestNG 项目,当我在执行测试用例后执行测试用例时,我使用 gmail smpt 和端口 465 调用在另一个类“SendMail.java”中编写的 SendMail 函数,它会自动附加输出中的“emailable-report”执行测试用例后的文件夹,它最初成功运行了几次,但突然我得到了下面显示的异常

DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  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.openServer(SMTPTransport.java:1961)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at automationframework.SendMail.sendMail(SendMail.java:134)
    at automationframework.SendMail.execute(SendMail.java:22)
    at automationframework.Iparkit_Login.sendReportMail(Iparkit_Login.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:308)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
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:549)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:354)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:211)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 24 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)
    ... 35 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)

这是我的 SendMail.java 类

package automationframework;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

    public class SendMail

    
    //reportFileName = TestExecutionResultFileName
    public static void execute(String reportFileName) throws Exception

    
    String path="D:\\K - Selenium\\SeleniumWorkspace\\iParkit-copy\\test-output\\emailable-report.html";

    String[] to="receipient@gmail.com";
    String[] cc=;
    String[] bcc=;//"<AutomationTester>";

    SendMail.sendMail("sender@gmail.com",
    "*******",
    "smtp.gmail.com",
    "465",
    "true",
    "true",
    true,
    "javax.net.ssl.SSLSocketFactory",
    "false",
    to,
    cc,
    bcc,
    "Test Execution Reports",
    "Please find attached Test Suite Execution Report",
    path,
    reportFileName);
    

    public static boolean sendMail(String userName,
    String passWord,
    String host,
    String port,
    String starttls,
    String auth,
    boolean debug,
    String socketFactoryClass,
    String fallback,
    String[] to,
    String[] cc,
    String[] bcc,
    String subject,
    String text,
    String attachmentPath,
    String attachmentName)

    //Object Instantiation of a properties file.
    Properties props = new Properties();

    props.put("mail.smtp.user", userName);

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

    if(!"".equals(port))
    props.put("mail.smtp.port", port);
    

    if(!"".equals(starttls))
    props.put("mail.smtp.starttls.enable",starttls);
    props.put("mail.smtp.auth", auth);
    

    if(debug)

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

    else

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

    

    if(!"".equals(port))
    props.put("mail.smtp.socketFactory.port", port);
    
    if(!"".equals(socketFactoryClass))
    props.put("mail.smtp.socketFactory.class",socketFactoryClass);
    
    if(!"".equals(fallback))
    props.put("mail.smtp.socketFactory.fallback", fallback);
    

    try

    Session session = Session.getDefaultInstance(props, null);

    session.setDebug(debug);

    MimeMessage msg = new MimeMessage(session);

    msg.setText(text);

    msg.setSubject(subject);

    Multipart multipart = new MimeMultipart();
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource(attachmentPath);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(attachmentName);
    multipart.addBodyPart(messageBodyPart);

    msg.setContent(multipart);
    msg.setFrom(new InternetAddress(userName));

    for(int i=0;i<to.length;i++)
    msg.addRecipient(Message.RecipientType.TO, new
    InternetAddress(to[i]));
    

    for(int i=0;i<cc.length;i++)
    msg.addRecipient(Message.RecipientType.CC, new
    InternetAddress(cc[i]));
    

    for(int i=0;i<bcc.length;i++)
    msg.addRecipient(Message.RecipientType.BCC, new
    InternetAddress(bcc[i]));
    

    msg.saveChanges();

    Transport transport = session.getTransport("smtp");

    transport.connect(host, userName, passWord);

    transport.sendMessage(msg, msg.getAllRecipients());

    transport.close();

    return true;

     catch (Exception mex)
    mex.printStackTrace();
    return false;
    
    




请帮助我自 2 天以来一直在苦苦挣扎,但没有任何解决方案适合我

【问题讨论】:

见JavaMail FAQ。 我见过但无济于事的人 一个重要的事情是我的另一台机器上运行相同的代码 FAQ 条目描述了它可能在一台机器上运行但在另一台机器上无法运行的几个原因。大多数原因与您编写的代码无关。请解释您在阅读 FAQ 条目后所做的一切,以及您得到的结果。也可以remove all the socket factory stuff。 【参考方案1】:

最后我自己解决了我的问题,我运行了 Avast Anti Virus,我把它关掉了,现在它就像魅力一样工作......

【讨论】:

嗯,呵呵。常见问题解答不是说这是一种可能性吗?是的,确实如此。 确实如此,但正如我告诉你的那样,我在另一台笔记本电脑上执行了相同的代码,突然意识到我的另一台笔记本电脑没有运行任何防病毒软件,是的,常见问题解答也有帮助

以上是关于TestNG 电子邮件报告 - PKIX 路径构建异常的主要内容,如果未能解决你的问题,请参考以下文章

Centos 7 中的 PKIX 路径构建失败

PKIX 路径构建失败:

PKIX 路径构建失败:wso2 oauth

PKIX 路径构建失败:无法找到请求目标的有效证书路径

PKIX 路径构建失败:我将证书添加到 carcert 仍然失败

SSLHandshakeException:PKIX 路径构建失败