Jakarta Mail throws MessagingException: Could not convert socket to TLS
jakarta.mail.MessagingException: Could not convert socket to TLSShort answer
The server accepted STARTTLS, but the TLS handshake that followed failed on your side. Turn on mail debug output and read the exception under the message. It is nearly always an untrusted certificate, something between you and the server that rewrites the connection, or a TLS protocol list that does not match.
Fix it fast
1Find out which cause you have
Switch on the protocol trace and read the nested exception that follows the message; it names the real handshake failure.
Properties props = new Properties(); props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.starttls.enable", "true"); Session session = Session.getInstance(props); session.setDebug(true);2Match what you see, then jump to its fix
- 1
The nested exception says unable to find valid certification path to requested target.The server certificate is not trusted by the JVMFix - 2
The same code works from another network, or the certificate shown in the debug trace is not your mail provider's.A firewall, antivirus or proxy rewrites the TLS connectionFix - 3
The nested exception is a handshake failure about protocol or cipher, not about a certificate path.The enabled TLS protocols do not match what the server acceptsFix
- 1
What does the error look like?
jakarta.mail.MessagingException: Could not convert socket to TLSjavax.mail.MessagingException: Could not convert socket to TLSunable to find valid certification path to requested target
Cause 1 of 3
The server certificate is not trusted by the JVM
1Check
The mail library follows the JVM trust store. With STARTTLS enabled, that store has to be set up correctly. A self-signed or private-CA certificate, or an application server that replaces the JDK's trusted authorities, produces this error.
ls -l "$JAVA_HOME/lib/security/cacerts"2Fix
For a server you control, trust that one host in the session properties. Prefer importing its certificate into the trust store the JVM really uses, and avoid the * wildcard in production.
props.put("mail.smtp.ssl.trust", "smtp.example.com");
// or point the JVM at a trust store that contains the certificate
// java -Djavax.net.ssl.trustStore=/etc/ssl/mail-truststore.jks MyApp3Verify
Send again with debug on; the STARTTLS handshake completes and the session moves on to login.
java -Djavax.net.ssl.trustStore=/etc/ssl/mail-truststore.jks MyAppConfirmed by eclipse-ee4j.github.io
Cause 2 of 3
A firewall, antivirus or proxy rewrites the TLS connection
1Check
The Jakarta Mail FAQ lists a firewall or antivirus that intercepts the connection as a cause of certificate path failures. Run the same code from another network or machine; if it works there, something on the first network sits in the middle.
java -Dmail.debug=true MyApp2Fix
Ask for the mail host to be excluded from TLS inspection, or put the inspection tool's CA certificate in a trust store file and start the JVM with it.
java -Djavax.net.ssl.trustStore=/etc/ssl/mail-truststore.jks MyApp3Verify
The same send now passes the handshake and reaches the login step in the trace.
java -Dmail.debug=true -Djavax.net.ssl.trustStore=/etc/ssl/mail-truststore.jks MyAppConfirmed by eclipse-ee4j.github.io
Cause 3 of 3
The enabled TLS protocols do not match what the server accepts
1Check
mail.smtp.ssl.protocols sets the protocols enabled for SSL connections as a whitespace-separated list, and the JVM can also restrict them: jdk.tls.client.protocols replaces the client defaults with the list you give.
grep -rn "ssl.protocols\|jdk.tls.client.protocols" src/ pom.xml run.sh 2>/dev/null2Fix
Remove stale overrides first. If you must set one, list a protocol the server really supports, for example TLSv1.2.
props.put("mail.smtp.ssl.protocols", "TLSv1.2");3Verify
Run the send again with debug on and watch the handshake finish.
java -Dmail.debug=true MyAppConfirmed by eclipse-ee4j.github.io
How do I stop it coming back?
- Keep a debug switch (setDebug or -Dmail.debug=true) in your app's config so the nested exception is one flag away.
- Do not trust all hosts with the * wildcard outside a test machine.
- Test from the machine that runs the app, not only from your laptop; the network in between can differ.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Gmail SMTP login fails with 535 or 534 5.7.14
534-5.7.14 Please log in via your web browser and then try again.
Gmail turned your sign-in down.
3 causes, each with a fixDjango send_mail fails or hangs with an SSL error
smtplib.SMTPException raised from send_mail()
Django has two secure modes that must not be mixed: EMAIL_USE_TLS for explicit TLS (STARTTLS, usually port 587) and EMAIL_USE_SSL for implicit TLS (usually port 465).
4 causes, each with a fixDovecot refuses to start: Can't load SSL certificate, no valid PEM certificate
doveconf: Error: ssl enabled, but ssl_cert not set
Dovecot reads the ssl_cert setting as the file's contents, not a path, so in 2.3 the value must start with a less-than sign.
3 causes, each with a fixCodeIgniter email does not send and shows no error message
$email->printDebugger() prints an empty result
CodeIgniter signals a failed email only by send() returning false, with no exception.
4 causes, each with a fixDKIM shows 'bad version' or 'unknown key type' because the DNS key record is malformed
dkim=fail (unknown key type)
These results mean the receiver found your DKIM key record but could not use it.
4 causes, each with a fixSmtpException: Unable to read data from the transport connection (net_io_connectionclosed)
System.Net.Mail.SmtpException: Unable to read data from the transport connection: net_io_connectionclosed.
The server closed the connection while SmtpClient was waiting for a reply.
4 causes, each with a fix