title: Unable to find valid certification path to requested target
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4062307/pkix-path-building-failed-unable-to-find-valid-certification-path-to-requested">PKIX path building failed: unable to find valid certification path to requested target</a>  </p>
  </blockquote>
  
  
  
  <p>I have a problem. Already struggling with it for a long time.
  Here is my code:</p>
  
  <pre><code>    package com.XYZ.spr.server;
  
  import org.apache.commons.mail.Email;
  import org.apache.commons.mail.SimpleEmail;
  
  public class GeneratorMaili {
      public static final String HOSTNAME = "smtp.XYZ.com";
      public static final String ADDRESS_FROM = "user@XYZ.com";
      public static final String TOPIC= "Topic";
  
      public static void wyslijEmail(String reciver)  throws Exception {
          System.setProperty("javax.net.ssl.trustStore", "cacerts");
          System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
          Email email = new SimpleEmail();
          email.setSmtpPort(587);
  //      email.setTLS(true);
          email.setSSL(true);
          System.out.println("fdfdf");
          email.setAuthentication("user@.XYZ.pl", "pass");
          email.setDebug(true);
          email.setHostName(HOSTNAME);
          email.setFrom(ADDRESS_FROM);
          email.setSubject(TOPIC);
          email.setMsg("Test");
          email.addTo(reciver);
          System.out.println(reciver);
          email.send();
          System.out.println("Ok!");
      }
  }
  </code></pre>
  
  <p>I have a problem with the certificate.
  Here are the steps that I have done:</p>
  
  <ol>
  <li>I have download certificate using my browser, and I have saved it as cert.cer</li>
  <li>I've used this command: keytool -import -trustcacerts -keystore cacerts -storepass changeit -noprompt -file cert.cer</li>
  <li>I use GWT, so I copied cacerts to my WEB-INF folder.</li>
  </ol>
  
  <p>Unfortunately, getting still getting this error:</p>
  
  <pre><code>    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
  org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.XYZ.com:587
      at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
      at org.apache.commons.mail.Email.send(Email.java:1267)
      at com.XYZ.spr.server.GeneratorMaili.wyslijEmail(GeneratorMaili.java:28)
      at com.XYZ.spr.server.SprawozdaniaServiceImpl.addSprawozdanie(SprawozdaniaServiceImpl.java:123)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
      at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
      at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
      at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
      at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
      at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
      at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
      at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
      at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
      at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
      at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
      at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
      at org.mortbay.jetty.Server.handle(Server.java:324)
      at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
      at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
      at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
      at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:205)
      at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
      at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
      at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
  Caused by: 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:1611)
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1369)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
      at javax.mail.Service.connect(Service.java:310)
      at javax.mail.Service.connect(Service.java:169)
      at javax.mail.Service.connect(Service.java:118)
      at javax.mail.Transport.send0(Transport.java:188)
      at javax.mail.Transport.send(Transport.java:118)
      at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
      ... 30 more
  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 com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source)
      at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
      at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
      at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source)
      at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
      at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
      at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
      at com.sun.net.ssl.internal.ssl.AppInputStream.read(Unknown Source)
      at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110)
      at java.io.BufferedInputStream.fill(Unknown Source)
      at java.io.BufferedInputStream.read(Unknown Source)
      at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:88)
      at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1589)
      ... 38 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(Unknown Source)
      at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
      at sun.security.validator.Validator.validate(Unknown Source)
      at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(Unknown Source)
      at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
      at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
      ... 51 more
  Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
      at java.security.cert.CertPathBuilder.build(Unknown Source)
      ... 57 more
  </code></pre>