Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javamail Could not convert socket to TLS GMail

I am trying to send an email using JavaMail through gmails SMTP Server. however this code.

final String username = "[email protected]";     final String password = "mygmailpassword";      Properties props = new Properties();     props.put("mail.smtp.auth", true);     props.put("mail.smtp.starttls.enable", true);     props.put("mail.smtp.host", "smtp.gmail.com");     props.put("mail.smtp.port", "587");      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("[email protected]"));         message.setRecipients(Message.RecipientType.TO,                 InternetAddress.parse("[email protected]"));         message.setSubject("Testing Subject");         message.setText("Dear Mail Crawler,"                 + "\n\n No spam to my email, please!");          Transport.send(message);          System.out.println("Done");      } catch (MessagingException e) {         throw new RuntimeException(e);     } 

Returns an this error

Could not convert socket to TLS; 

The Complete Stacktrace

Exception in thread "main" 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 
like image 631
user962206 Avatar asked Apr 20 '13 01:04

user962206


People also ask

Could not convert socket to TLS error?

"Could not convert socket to TLS" errors, when configuring a connection with an SMTP server, can be caused when trying to connect to a non-SSL port or when the SSL certificate use by the SMTP server is not trusted.

What is mail SMTP StartTLS enable?

StartTLS is a protocol command used to inform the email server that the email client wants to upgrade from an insecure connection to a secure one using TLS or SSL. StartTLS is used with SMTP and IMAP, while POP3 uses the slightly different command for encryption, STLS.

What is SMTP in Java?

SMTP is an acronym for Simple Mail Transfer Protocol. It is an Internet standard for electronic mail (e-mail) transmission across Internet Protocol (IP) networks.


2 Answers

props.put("mail.smtp.ssl.trust", "smtp.gmail.com"); 
like image 186
carlos Avatar answered Sep 24 '22 20:09

carlos


I disabled avast antivirus for 10 minutes and get it working.

like image 37
Shahid Abbasi Avatar answered Sep 22 '22 20:09

Shahid Abbasi