title: Javamail Could not convert socket to TLS GMail
body: <p>I'm trying to send an email using JavaMail through Gmail SMTP Server. This is the code:</p>
  <pre><code>final String username = &quot;mygmail@gmail.com&quot;;
  final String password = &quot;mygmailpassword&quot;;
  
  Properties props = new Properties();
  props.put(&quot;mail.smtp.auth&quot;, true);
  props.put(&quot;mail.smtp.starttls.enable&quot;, true);
  props.put(&quot;mail.smtp.host&quot;, &quot;smtp.gmail.com&quot;);
  props.put(&quot;mail.smtp.port&quot;, &quot;587&quot;);
  
  Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(username, password);
          }
      }
  );
  
  try {
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress(&quot;no-reply@gmail.com&quot;));
      message.setRecipients(Message.RecipientType.TO,
                          InternetAddress.parse(&quot;test@gmail.com&quot;));
      message.setSubject(&quot;Testing Subject&quot;);
      message.setText(&quot;Dear Mail Crawler,&quot; + &quot;\n\n No spam to my email, please!&quot;);
      Transport.send(message);
      System.out.println(&quot;Done&quot;);
  } catch (MessagingException e) {
      throw new RuntimeException(e);
  }
  </code></pre>
  <p>Returns an this error:</p>
  <blockquote>
  <p>Could not convert socket to TLS;</p>
  </blockquote>
  <p>The complete stacktrace:</p>
  <blockquote>
  <p>Exception in thread &quot;main&quot; java.lang.RuntimeException: javax.mail.MessagingException: Could not convert socket to TLS;
  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 Test.main(Test.java:43)
  Caused by: javax.mail.MessagingException: Could not convert socket to TLS;
  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.startTLS(SMTPTransport.java:1907)
  at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)
  at javax.mail.Service.connect(Service.java:317)
  at javax.mail.Service.connect(Service.java:176)
  at javax.mail.Service.connect(Service.java:125)
  at javax.mail.Transport.send0(Transport.java:194)
  at javax.mail.Transport.send(Transport.java:124)
  at Test.main(Test.java:38)
  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(Alerts.java:174)
  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1649)
  at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:241)
  at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:235)
  at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1206)
  at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:136)
  at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:593)
  at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:529)
  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:893)
  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)
  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1165)
  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1149)
  at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
  at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:486)
  at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
  ... 7 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(PKIXValidator.java:323)
  at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:217)
  at sun.security.validator.Validator.validate(Validator.java:218)
  at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:126)
  at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:209)
  at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:249)
  at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1185)
  ... 17 more
  Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:174)
  at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:238)
  at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:318)
  ... 23 more</p>
  </blockquote>