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

时间:2015-12-25 00:12:57

标签: java ssl-certificate testng javamail sslhandshakeexception

我突然在一个问题场景中运行了 在执行测试用例后执行测试用例时,我在selenium中运行TestNG项目我调用另一个类中编写的SendMail函数" SendMail.java"使用gmail smpt&端口465它自动附加" 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天以来一直在努力,但没有解决方案适合我

1 个答案:

答案 0 :(得分:1)

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