When SMTP client connects to remote SMTP server and issues STARTTLS command, which has self-signed certificate - I get the error in client side:
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
There is one solution to trust all certificates like:
SSLContext trustAllSSLContext;
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
try {
trustAllSSLContext = SSLContext.getInstance("SSL");
trustAllSSLContext.init(null, trustAllCerts, null);
} catch (NoSuchAlgorithmException | KeyManagementException ex) {
//...
}
But this causes security vulnerabilities. I think maybe this is better than to send unencrypted data in this case?