简体   繁体   中英

getting PKIX path build failed

i'm trying to send mail using java through gmail but im getting some exceptions The Code is follows:

 public static void send_java_mail() throws IOException
{
    System.out.println("\n\n\n\nIn send_java_mail");
    String[] recipents=new String[10];
    recipents[0]=email_id;//email_id;       
    //put your smtp details in the properties file.Following given are for gmail account

    Properties smtp_properties =null;
    smtp_properties = new Properties();
    InputStream iss = new FileInputStream("D:\\Heat\\Web\\smtpconfig.properties");
    smtp_properties.load(iss);


    String e_mailbody="Hi\n\tYour Order has been Placed.It will deliever in 2 Days.\n\n\tYour Name :"+CustomerName+"\n\tYour Email Address :"+email_id+"\n\tYour Shipping Address :"+Addr+"\n\tYour Contact Number:"+Phone+"\n\tYour Total amount to be Pay is 3610.\nThanks.";
    String host = smtp_properties.getProperty("host");
    String mail_transport_protocol = smtp_properties.getProperty("mail.transport.protocol");
    String mail_smtp_auth =smtp_properties.getProperty("mail.smtp.auth");
    String mail_smtp_port = smtp_properties.getProperty("mail.smtp.port");
    String mail_debug =smtp_properties.getProperty("mail.debug");
    String mail_smtp_socketFactory_port =smtp_properties.getProperty("mail.smtp.socketFactory.port");
    String mail_smtp_socketFactory_class =smtp_properties.getProperty("mail.smtp.socketFactory.class");
    String mail_smtp_socketFactory_fallback = smtp_properties.getProperty("mail.smtp.socketFactory.fallback");

    Properties props = System.getProperties();
    System.out.println("mail transport protocol :"+mail_transport_protocol);
    props.setProperty("mail.transport.protocol",mail_transport_protocol);     
    props.setProperty("mail.host", host);  
    props.put("mail.smtp.auth", mail_smtp_auth);  
    props.put("mail.smtp.port", mail_smtp_port);  
    props.put("mail.debug", mail_debug);
    props.setProperty("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", mail_smtp_socketFactory_port);  
    props.put("mail.smtp.socketFactory.class",mail_smtp_socketFactory_class);  
    props.put("mail.smtp.socketFactory.fallback", mail_smtp_socketFactory_fallback);  

    final String from="dhanshreeherballife@gmail.com";
    final String pass="D@ny1996";

    Session session = Session.getDefaultInstance(props,  new javax.mail.Authenticator() {    
        protected PasswordAuthentication getPasswordAuthentication() {    
            return new PasswordAuthentication(from,pass);  
            }    
           });  
    MimeMessage message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[recipents.length];

            toAddress[0] = new InternetAddress(recipents[0]);

            message.addRecipient(Message.RecipientType.TO, toAddress[0]);


            message.setSubject("Order");
        message.setText(e_mailbody);
        Transport transport = session.getTransport("smtp");

        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }               
}

The exception is as follows:

DEBUG SMTP: exception reading response: 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
javax.mail.MessagingException: Exception reading response;
  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.readServerResponse(SMTPTransport.java:1090)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:986)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:197)
    at javax.mail.Service.connect(Service.java:233)
    at javax.mail.Service.connect(Service.java:134)
    at Servlets.Sendmail.send_java_mail(Sendmail.java:92)
    at Servlets.PlaceOrder.doGet(PlaceOrder.java:64)
    at Servlets.PlaceOrder.doPost(PlaceOrder.java:72)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

Start by fixing all these common JavaMail mistakes .

The JavaMail FAQ has more information on cert path errors . If you're getting this error when connecting to Gmail, it's most likely that you have a firewall or anti-virus product that's intercepting the connection and presenting its own certificate instead of the Gmail certificate. You should think carefully about the security implications of allowing that software to monitor your connection before using any of the techniques in the link above to disable the security checks.

You should also check that your JDK installation isn't misconfigured or corrupt. Perhaps your application server is overriding the cacerts file (trust store) that comes with the JDK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM