Jakarta Mail cannot connect to smtp.gmail.com on port 465 (response: -1)
Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1Short answer
On port 465 the server expects TLS from the first byte, but Jakarta Mail starts in plain SMTP unless told otherwise. Set mail.smtp.ssl.enable to true for 465, or use port 587 with STARTTLS. Do not mix the two, and set the port where connect uses it.
Fix it fast
1Find out which cause you have
Print the settings your code really passes and turn on the trace. The first lines show which host and port were used and whether SSL was on.
session.setDebug(true); // run with: java -Dmail.debug=true MyApp2Match what you see, then jump to its fix
- 1
The port is 465 and the properties contain starttls or no SSL setting at all.Port 465 is used without mail.smtp.ssl.enableFix - 2
The port is 587 and mail.smtp.ssl.enable is true.SSL is switched on for port 587, which uses STARTTLSFix - 3
The code sets mail.smtp.port, but the trace shows port 25, or you use the smtps protocol with mail.smtp.* names.The port or properties are not the ones the connection usesFix
- 1
What does the error look like?
Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1Got bad greeting from SMTP host
Cause 1 of 3
Port 465 is used without mail.smtp.ssl.enable
1Check
Port 465 expects SSL from the start of the connection. The Jakarta Mail FAQ says to set mail.smtp.ssl.enable to true, remove mail.smtp.starttls.enable, and set the port to 465.
grep -rn "smtp\.\(ssl\|starttls\|port\)" src/2Fix
Use implicit SSL for 465, with the host and port set explicitly.
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.auth", "true");3Verify
The trace shows the connection to port 465 and a greeting from the server.
java -Dmail.debug=true MyAppConfirmed by eclipse-ee4j.github.io
Cause 2 of 3
SSL is switched on for port 587, which uses STARTTLS
1Check
Google lists port 465 for SSL and port 587 for TLS. Port 587 starts in plain SMTP and upgrades with STARTTLS, so a client that begins with SSL there fails the same way.
grep -rn "smtp.port\|smtp.ssl.enable" src/2Fix
For 587, drop the SSL switch and use STARTTLS instead.
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");3Verify
The trace shows a STARTTLS exchange before the login.
java -Dmail.debug=true MyAppConfirmed by knowledge.workspace.google.com
Cause 3 of 3
The port or properties are not the ones the connection uses
1Check
mail.smtp.port applies only when connect does not give a port, and its default is 25. For the smtps protocol, the property names start with mail.smtps instead.
grep -rn "connect(\|getTransport\|mail.smtps" src/2Fix
Pass the port in connect, or use one protocol name consistently across the property names and the transport.
Transport t = session.getTransport("smtp");
t.connect("smtp.gmail.com", 465, username, password);3Verify
The trace shows the host and port from the connect call.
java -Dmail.debug=true MyAppConfirmed by eclipse-ee4j.github.io
How do I stop it coming back?
- Keep one table of port to mode in your config: 465 is SSL, 587 is STARTTLS.
- Read the settings from one place so the port and the SSL flags cannot drift apart.
- Use an app password or OAuth for Gmail; Google no longer accepts a plain account password from apps on Workspace accounts.
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.
Related fixes
Jakarta Mail throws MessagingException: Could not convert socket to TLS
javax.mail.MessagingException: Could not convert socket to TLS
The server accepted STARTTLS, but the TLS handshake that followed failed on your side.
3 causes, each with a fixGmail 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 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 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 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