"Connection refused" and "connection timed out" are different failures and point to different places. Connection refused means your connection attempt reached a machine (or a firewall configured to reject) and was actively turned away: nothing is listening on that port, or a rule sends back a reset. Connection timed out means no answer came back at all, which usually means packets are being silently dropped by a firewall, a routing problem or a blocked network path, or that the host is down or unreachable.
Neither message comes from SMTP itself. They occur before the server's 220 greeting, at the TCP layer, so no SMTP reply code exists to read. The method is to work outward in a fixed order: name resolution, network path, port, then TLS mode and the application settings. This guide gives that order, with commands for Linux, macOS and Windows.
Refused versus timed out: what each tells you
| Symptom | What probably happened | Where to look |
|---|---|---|
| Connection refused immediately | A host answered with a reset: no service on that port, or a reject rule | Service status and listening ports on the server, port number in your configuration |
| Connection timed out after tens of seconds | No answer: packets dropped on the way out or in | Outbound firewall, ISP or cloud blocks, server firewall, routing |
| Hangs after connecting, no 220 banner | TCP connected but TLS mode is wrong, or the server is overloaded or tarpitting | STARTTLS versus implicit TLS, server load. See SMTP tarpit |
| Name or service not known | DNS failure, not a connection error | Hostname spelling and DNS resolver |
| Network is unreachable | No route, often IPv6 without connectivity | IPv6 settings and default route |
| Connects, then closed with 421 | Server accepted TCP then refused service | See SMTP 421 |
Step 1: confirm the hostname resolves
A wrong or stale name sends you to a machine that was never an SMTP server. Resolve the name from the same machine that fails, not your laptop.
dig +short smtp.example.com A
dig +short smtp.example.com AAAA
# Compare with what the system resolver returns
getent hosts smtp.example.comIf the A record points to an address you do not recognise, or no record exists, fix the name or DNS before looking at anything else. Remember that the host for sending is your provider's submission host, not the MX host of the recipient. If you changed DNS recently, caching can delay the change. See DNS TTL for email. Having both A and AAAA records matters in the next step.
Step 2: test the exact port with a raw TCP tool
Use a tool that does nothing but connect. If it fails, your application is not the problem.
# Linux or macOS
nc -vz smtp.example.com 587
nc -vz smtp.example.com 465
nc -vz smtp.example.com 25
# Windows PowerShell
Test-NetConnection -ComputerName smtp.example.com -Port 587
# Show the SMTP banner (should print a 220 line)
openssl s_client -connect smtp.example.com:587 -starttls smtp -crlf
openssl s_client -connect smtp.example.com:465 -crlfA successful nc or Test-NetConnection proves the network path and port are open. Then the problem is in the SMTP or TLS layer. A failure means keep investigating the network. Test several ports: if 587 and 465 fail but ordinary web ports work, the network is filtering mail ports. See testing SMTP connections.
Step 3: check which ports your network blocks
Port 25 is the port most often blocked. Many consumer ISPs block outbound 25 to limit spam from infected machines, and many cloud providers restrict it by default on new accounts, sometimes with a request process to lift it. Submission ports 587 and 465 are usually open, but corporate networks, hotel and guest Wi-Fi and some mobile networks filter them too. Because policies differ and change, confirm your provider's current rules rather than assuming. See why port 25 is blocked.
- Test from the failing machine and from a different network, such as a mobile hotspot, to a known SMTP host on the same port.
- If it works elsewhere, the original network or host firewall blocks the port.
- Switch from port 25 to 587 or 465 when you are submitting mail as a client; port 25 is meant for server-to-server delivery.
- On a cloud server, check the provider's outbound mail policy and the security group or network ACL rules.
- If the corporate firewall blocks mail ports, ask the network team for an exception to the specific destination, not a broad rule.
Step 4: check the server side when you run it
If you own the receiving server, "connection refused" almost always means nothing is listening, and "timed out" means a firewall is in the way.
# Is the SMTP service listening, and on which addresses?
ss -ltnp | grep -E ':(25|465|587)\b'
# Is the service running?
systemctl status postfix
# Local firewall rules
ufw status verbose
iptables -S | grep -E 'dport (25|465|587)'
nft list ruleset | grep -E 'dport (25|465|587)'Look for services that listen only on 127.0.0.1 (not reachable from outside), submission ports that are not enabled in the MTA configuration (for Postfix, the submission and smtps entries in master.cf), and security groups that allow 25 but not 587. Also check connection limits and fail2ban or similar tools, which can ban a client that failed authentication repeatedly and make it look like a timeout. See SMTP firewall rules and SMTP brute force protection.
Step 5: match the TLS mode to the port
A TCP connection that succeeds and then hangs or resets is often a mismatch between the port and the TLS mode. Port 465 expects TLS from the first byte (implicit TLS). Port 587 starts in plain text and upgrades with STARTTLS. If a client uses STARTTLS against 465, it waits for a banner that never arrives and times out. If a client uses implicit TLS against 587, the server sends a plain text greeting that the client cannot parse and the handshake fails. See SSL, TLS and STARTTLS and SMTP settings reference.
| Port | TLS mode | Library setting |
|---|---|---|
| 587 | STARTTLS (explicit) | Plain connection then upgrade; often called TLS, STARTTLS or secure=false |
| 465 | Implicit TLS | Encrypted from the start; often called SSL, SMTPS or secure=true |
| 25 | Optional STARTTLS | Server to server; usually blocked for clients |
Step 6: IPv6 and slow fallbacks
If the hostname has an AAAA record and the machine has no working IPv6 route, some clients try IPv6 first, wait for the timeout and only then fall back to IPv4, giving long delays that end as a timeout. Test with explicit address families, and either fix the IPv6 path or configure the client to prefer IPv4. See IPv6 and email.
nc -4vz smtp.example.com 587
nc -6vz smtp.example.com 587
curl -4 -v --max-time 15 telnet://smtp.example.com:587Step 7: application level timeouts
If raw tests pass but your application still times out, look at its timeout settings. A default of a few seconds can be too short for a TLS handshake over a slow link, and DNS lookups inside containers can add seconds. Set separate connect and read timeouts, log the exact stage that fails, and retry with backoff rather than in a tight loop. Language-specific notes are in Python smtplib, Go net/smtp and connection timeout and TLS errors.
import smtplib, socket
try:
with smtplib.SMTP('smtp.example.com', 587, timeout=20) as s:
s.ehlo()
s.starttls()
s.ehlo()
print('connected and TLS ok')
except socket.timeout:
print('timed out: network path, firewall or TLS mode')
except ConnectionRefusedError:
print('refused: wrong port or nothing listening')
except socket.gaierror as e:
print('DNS failure:', e)Troubleshooting table
| Observation | Likely cause | Action |
|---|---|---|
| Refused on 587, works on 25 | Submission service not enabled on the server | Enable it in the MTA and open the firewall |
| Timed out on 25, works on 587 | ISP or cloud block on 25 | Use 587 or 465 for client submission |
| Works from home, times out from the data centre | Cloud or corporate outbound filter | Check security groups and provider policy |
| Works for hours, then refused | Rate limiting or ban after failures | Check fail2ban and connection limits. See backoff guidance |
| Long delay then success on retry | IPv6 first, IPv4 fallback | Fix IPv6 or prefer IPv4 |
| Hangs after connect | Wrong TLS mode for the port | Switch STARTTLS and implicit TLS to match the port |
Next steps
Write down the host, port and TLS mode for each integration, keep them in configuration rather than code and add a periodic connectivity probe that connects, completes the EHLO and quits, with an alert when it fails. When a failure happens, run the same short sequence every time: resolve, connect, banner, TLS, authenticate. It usually points to the cause in under ten minutes.
Reachable on the ports that are open everywhere Submit mail over 587 with STARTTLS or 465 with implicit TLS, using the same host from any network.
References and further reading
Frequently asked questions
What is the difference between connection refused and connection timed out?
Refused means a host answered and rejected the connection, such as nothing listening on that port. Timed out means no answer arrived, usually because a firewall dropped the packets.
Why is my SMTP connection timing out on port 25?
Many ISPs and cloud providers block outbound port 25. Use 587 or 465 for client submission and confirm your provider's policy.
Why do I get connection refused on port 587?
Nothing is listening on 587 at that host, the submission service is disabled, or a firewall rule rejects it. Verify the host and the MTA configuration.
How do I test an SMTP port?
Use nc -vz host port, Test-NetConnection on Windows, or openssl s_client with -starttls smtp to see the banner.
Can the wrong TLS setting cause a timeout?
Yes. Using STARTTLS on port 465 or implicit TLS on port 587 makes the client wait or fail the handshake.