JavaMail Email / SMTP Origin: Network OSI layer 7 · Application Last updated 18 Nov 2026

javax.mail.MessagingException: Could not connect to SMTP host: smtp.example.com, port: 587 (JavaMail)

JavaMail couldn't open a connection to the SMTP server.

Exact message you may see javax.mail.MessagingException: Could not connect to SMTP hostCould not connect to SMTP host: smtp.gmail.com, port: 587MailConnectException: Couldn't connect to host, port: smtp.example.com, 587; timeoutjakarta.mail.MessagingException: Could not connect to SMTP hostnested exception is: java.net.ConnectException: Connection refused
Seen on: Java SMTP

Meaning

Check host/port reachability, firewall, mail.smtp.starttls.enable=true for 587 or mail.smtp.ssl.enable=true for 465, and set timeouts (mail.smtp.connectiontimeout). The nested exception (ConnectException, SSLHandshakeException, UnknownHostException) holds the real cause; on newer JDKs add mail.smtp.ssl.protocols=TLSv1.2.

Common causes

  • Host/port blocked or wrong
  • STARTTLS/SSL property missing
  • DNS failure (UnknownHostException)
  • TLS protocol mismatch on newer JDKs
  • No timeouts set, hanging

⚡ Quick fix

  1. Read the nested exception
  2. Set mail.smtp.starttls.enable/mail.smtp.ssl.enable correctly
  3. Set mail.smtp.ssl.protocols=TLSv1.2

Detailed fix by platform

Java

  1. props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.ssl.protocols", "TLSv1.2"); props.put("mail.smtp.connectiontimeout", "10000");

How to diagnose

  1. Nested cause — Which?
  2. STARTTLS/SSL — Set?
  3. Firewall — Open?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Network. Errors that are often confused with or caused by this one (click to open):

421 4.7.0 Try again later (rate limited) — SMTP: 421 4.7.0 Our system has detected an unusual rate of unsolicited mail originating from your IP address. Mail sent from your IP address has been temporarily rate limited (Email / SMTP)421 4.7.0 Try again l…ActiveRecord::ConnectionNotEstablished — Rails: ActiveRecord::ConnectionNotEstablished: No connection pool for 'ActiveRecord::Base' found (Ruby / Rails)ActiveRecord::Connect…cipher suites — Java: SSLHandshakeException — no cipher suites in common (server side) (SSL / TLS)cipher suitesConnectionRefusedError [Errno 111] (SMTP) — Python: ConnectionRefusedError: [Errno 111] Connection refused — smtplib connect to localhost:25 (Email / SMTP)ConnectionRefusedErro…disabledAlgorithms — Java: SSLHandshakeException — TLSv1/TLSv1.1 or algorithm disabled by jdk.tls.disabledAlgorithms (SSL / TLS)disabledAlgorithmshostname — Java: SSLPeerUnverifiedException — Certificate for <host> doesn't match any of the subject alternative names (SSL / TLS)hostnameKeystore was tampered with, or password was incorrect — Java keytool: java.io.IOException: Keystore was tampered with, or password was incorrect (SSL / TLS)Keystore was tampered…No subject alternative names present — Java: javax.net.ssl.SSLHandshakeException: No subject alternative names matching IP address 10.0.0.5 found (SSL / TLS)No subject alternativ…JavaMail

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.