java - Javamail - Could not connect to SMTP host



I'm trying to send email from my java and I couldn't connect to my host. Here my codes:

public class sendEmail implements Runnable{
     public void run(){
            final String username = "user", password = "pass", from = "", to = "";

            Properties props = new Properties();
            props.setProperty("mail.smtp.user", username);
            props.setProperty("", "");
            props.setProperty("mail.smtp.ssl.enable", "true"); 
            props.setProperty("mail.smtp.port", "465");
            props.setProperty("mail.smtp.starttls.enable", "true");
            props.setProperty("mail.smtp.debug", "true");
            props.setProperty("mail.smtp.auth", "true");
            props.setProperty("mail.smtp.socketFactory.port", "465");
            props.setProperty("mail.smtp.socketFactory.class", "");
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, auth);

            Message msg = new MimeMessage(session);
                msg.setSubject("Test SMTP");
                msg.setFrom(new InternetAddress(from));
                msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
                Transport transport = session.getTransport("smtp");
                transport.connect("", username, password);
                transport.sendMessage(msg, msg.getAllRecipients());
            }catch(MessagingException | NumberFormatException | HeadlessException ex){
        }catch(UnknownHostException | NumberFormatException ex){

and here my SMTPAuthenticator() codes:

private class SMTPAuthenticator extends javax.mail.Authenticator {
    public PasswordAuthentication getPasswordAuthentication() {
        String username = "user"; 
        String password = "pass";
        return new PasswordAuthentication(username, password);

and here again, my report bugs :

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 "", port 465, isSSL true javax.mail.MessagingException: Could not connect to SMTP host:, port: 465; nested exception is: PKIX path building failed: unable to find valid certification path to requested target at com.sun.mail.smtp.SMTPTransport.openServer( at com.sun.mail.smtp.SMTPTransport.protocolConnect( at javax.mail.Service.connect( at javax.mail.Service.connect( at scam.ccChecker$ at Caused by: PKIX path building failed: unable to find valid certification path to requested target at at at at at at at at at at at at at com.sun.mail.util.SocketFetcher.configureSSLSocket( at com.sun.mail.util.SocketFetcher.createSocket( at com.sun.mail.util.SocketFetcher.getSocket( at com.sun.mail.smtp.SMTPTransport.openServer( ... 5 more Caused by: PKIX path building failed: unable to find valid certification path to requested target at at at at at at at ... 16 more Caused by: unable to find valid certification path to requested target at at at at ... 22 more

can someone help me? :(

2 Answers: 

You might want to try downloading the certificate, install it using keytool, then set them as system variables. Here's how:

  1. Go to URL in your firefox browser, click on HTTPS certificate chain (next to URL address). Click "more info" > "security" > "show certificate" > "details" > "export..". Pickup the name and choose file type example.der. Now you have file with keystore and you have to add it to your JVM
  2. Locate the file $JAVA_HOME/jre/lib/security/cacerts
  3. Import the example.der file into the cacerts file using the following:

    sudo keytool -import -alias example -keystore  $JAVA_HOME/jre/lib/security/cacerts -file example.der
  4. The default keystore password is 'changeit'

  5. You can view the change that you made with this command that shows the Certificate fingerprint.

    keytool -list -keystore cacerts
  6. If this doesn't solve the problem, try adding these java options as arguments:"$JAVA_HOME/jre/lib/security/cacerts""changeit"
  7. Or you can put them in as system variables in your java code:


More instructions can be found here:


Maybe you can try to set certificate, to access the SMTP port. Something like that: