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

**Code:** `JavaMail` · **Category:** Email / SMTP · **Seen on:** Java, SMTP

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

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/email/java-mail-could-not-connect-to-smtp-host)
