6

Possible Duplicate:
PKIX path building failed: unable to find valid certification path to requested target

I have a problem. Already struggling with it for a long time. Here is my 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!");
    }
}

I have a problem with the certificate. Here are the steps that I have done:

  1. I have download certificate using my browser, and I have saved it as cert.cer
  2. I've used this command: keytool -import -trustcacerts -keystore cacerts -storepass changeit -noprompt -file cert.cer
  3. I use GWT, so I copied cacerts to my WEB-INF folder.

Unfortunately, getting still getting this error:

    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
Community
  • 1
  • 1
Mateusz
  • 853
  • 4
  • 12
  • 19

1 Answers1

1

I use GWT, so I copied cacerts to my WEB-INF folder.

The Java truststore specified using the javax.net.ssl.trustStore property is not loaded using the classloader, and hence placing it in the classpath has no bearing on the results. You need to specify the absolute path to the truststore, in your System.setProperty invocation.

Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
  • Ok, I went on, but now I get the following error:DEBUG SMTP: exception reading response: javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty – Mateusz Sep 08 '11 at 13:14
  • According to [this page](http://jcalcote.wordpress.com/2009/06/19/java-https-key-setup/) the truststore path is still incorrect, for the new message is another crytpic way of saying that the truststore could not be opened. – Vineet Reynolds Sep 08 '11 at 13:30
  • I do not understand. I put absolute path to cacerts ( with C:\...) and I recive this error again "unable to find valid certification path to requested target". I think I tested every possible path. – Mateusz Sep 08 '11 at 14:03