1

When sending mail using Jakarta email throwing below exception

Using these dependencies jakarta.activation-2.0.1.jar jakarta.mail-2.0.2

jakarta.mail.MessagingException: Could not convert socket to TLS;
nested exception is: java.net.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2140) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:734) at jakarta.mail.Service.connect(Service.java:364) at jakarta.mail.Service.connect(Service.java:222) at jakarta.mail.Service.connect(Service.java:171) at jakarta.mail.Transport.send0(Transport.java:230) at jakarta.mail.Transport.send(Transport.java:100) at test.GmailSSLTest.main(GmailSSLTest.java:44)

Tried below code in local to send mail using jakarta email using port 587, but its throwing above mentioned exception

package test; import jakarta.mail.*; import jakarta.mail.internet.*; import java.util.Properties; public class GmailTest { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.starttls.required", "true"); props.put("mail.smtp.ssl.enable", "false"); props.put("mail.smtp.ssl.protocols", "TLSv1.2"); props.put("mail.debug", "true"); Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( "*****@gmail.com", "******" ); } }); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("*****@gmail.com")); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("*****@gmail.com")); msg.setSubject("Test mail"); msg.setText("Hello from Jakarta Mail via Gmail 587 STARTTLS!"); Transport.send(msg); System.out.println("Message sent!"); } } 
4
  • If you’re using your normal Gmail password, switch to a Google App Password (generate it in your Google account settings) and be sure to use a valid address like [email protected] (not [email protected]@gmail.com). If those are correct, check that outbound port 587 is reachable through your firewall or network. Commented Sep 26 at 7:45
  • I have used Google App Password using valid email address but still facing the same issue. Commented Sep 29 at 4:25
  • Also tried this command to check openssl s_client -starttls smtp -connect smtp.gmail.com:587 .. getting this response CONNECTED(00000004) write:errno=113 --- no peer certificate available --- No client certificate CA names sent --- SSL handshake has read 273 bytes and written 359 bytes Verification: OK --- New, (NONE), Cipher is (NONE) Secure Renegotiation IS NOT supported Compression: NONE Expansion: NONE No ALPN negotiated Early data was not sent Verify return code: 0 (ok) Commented Sep 29 at 4:32
  • Try app password with -Djavax.net.debug=ssl Commented Sep 29 at 7:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.