Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Email delivery

Jakarta Mail cannot connect to smtp.gmail.com on port 465 (response: -1)

ErrorCould not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

Short 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.

  • Applies to Jakarta Mail 2.x, Angus Mail 2.0, JDK 17, JDK 21
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 MyApp
  2. 2Match what you see, then jump to its fix

    1. 1The port is 465 and the properties contain starttls or no SSL setting at all.Port 465 is used without mail.smtp.ssl.enableFix
    2. 2The port is 587 and mail.smtp.ssl.enable is true.SSL is switched on for port 587, which uses STARTTLSFix
    3. 3The 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

What does the error look like?

  • Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
  • Got 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 MyApp

Confirmed by eclipse-ee4j.github.io

Did this fix it?

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 MyApp

Confirmed by knowledge.workspace.google.com

Did this fix it?

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 MyApp

Confirmed by eclipse-ee4j.github.io

Did this fix it?

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

Email

Django 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.