Python: ConnectionRefusedError: [Errno 111] Connection refused — smtplib connect to localhost:25
Nothing is listening on the SMTP host and port your script connected to — often localhost:25 because no host was given and no local mail server runs.
ConnectionRefusedError: [Errno 111] Connection refused smtplib[Errno 111] Connection refused SMTPConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused itsmtplib connection refused localhost 25Errno 61 Connection refused smtp Meaning
smtplib.SMTP() defaults to localhost on port 25. On servers and containers there is usually no local MTA. Configure the real SMTP host, or install a local relay such as Postfix. Also check firewall rules and that the port number matches the service.
Common causes
- No host passed, so it connects to localhost:25
- Local MTA (Postfix) not installed or stopped
- Wrong port number for the provider
- Firewall/security group blocking outbound SMTP
- Container network without a mail service
⚡ Quick fix
- Pass the real host and port:
smtplib.SMTP("smtp.example.com", 587) - Start Postfix or use an ESP
- Test connectivity:
nc -vz smtp.example.com 587
Detailed fix by platform
Linux
ss -ltnp | grep :25shows whether a local MTA listens;nc -vz host 587tests a remote port
How to diagnose
- Host — Explicit or default localhost?
- Listener — Service running?
- Firewall — Port allowed outbound?
🔧 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
- 450 4.7.1 Greylisted, please try again later SMTP: 450 4.7.1 <user@example.com>: Recipient address rejected: Greylisted, see http://postgrey.schweikert.ch/help/example.com.html
- Nodemailer: ETIMEDOUT / ECONNECTION Nodemailer: Error: connect ETIMEDOUT 142.250.27.108:465 (code: 'ETIMEDOUT', command: 'CONN')
Most viewed in Email / SMTP
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 4 Nov 2026