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.
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 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
- Read the nested exception
- Set
mail.smtp.starttls.enable/mail.smtp.ssl.enablecorrectly - Set
mail.smtp.ssl.protocols=TLSv1.2
Detailed fix by platform
Java
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
- Nested cause — Which?
- STARTTLS/SSL — Set?
- 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:
Similar errors
- 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
- ActiveRecord::ConnectionNotEstablished Rails: ActiveRecord::ConnectionNotEstablished: No connection pool for 'ActiveRecord::Base' found
- cipher suites Java: SSLHandshakeException — no cipher suites in common (server side)
Most viewed in Email / SMTP
- 550 5.7.1 550 5.7.1 Relaying denied / Message rejected as spam / Unauthenticated email not accepted
- Laravel: Connection could not be established with host Laravel: Symfony\Component\Mailer\Exception\TransportException: Connection could not be established with host "smtp.mailtrap.io:2525": stream_socket_client(): Unable to connect
- SES: Maximum sending rate exceeded Amazon SES: Throttling — Maximum sending rate exceeded (454 Throttling failure)
Other ways to find it
Where this error sits
Origin: Network. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 18 Nov 2026