ConnectionRefusedError [Errno 111] (SMTP) Email / SMTP Origin: Network Last updated 4 Nov 2026

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.

Exact message you may see 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
Seen on: SMTP Python Linux

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

  1. Pass the real host and port: smtplib.SMTP("smtp.example.com", 587)
  2. Start Postfix or use an ESP
  3. Test connectivity: nc -vz smtp.example.com 587

Detailed fix by platform

Linux

  1. ss -ltnp | grep :25 shows whether a local MTA listens; nc -vz host 587 tests a remote port

How to diagnose

  1. Host — Explicit or default localhost?
  2. Listener — Service running?
  3. 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:

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…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 (Email / SMTP)450 4.7.1 Greylisted,…Nodemailer: ETIMEDOUT / ECONNECTION — Nodemailer: Error: connect ETIMEDOUT 142.250.27.108:465 (code: 'ETIMEDOUT', command: 'CONN') (Email / SMTP)Nodemailer: ETIMEDOUT…Outbound port 25 blocked — SMTP: telnet smtp.example.com 25 — Connection timed out (cloud provider blocks port 25) (Email / SMTP)Outbound port 25 bloc…Redis connection refused (Laravel) — Laravel: Predis\Connection\ConnectionException: Connection refused [tcp://127.0.0.1:6379] / RedisException: Connection refused (Laravel)Redis connection refu…sendmail: not found (PHP mail) — PHP: sh: 1: /usr/sbin/sendmail: not found — mail() returns false (Email / SMTP)sendmail: not found (…SMTPAuthenticationError (Python) — Python: smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted') (Email / SMTP)SMTPAuthenticationErr…SMTPServerDisconnected: Connection unexpectedly closed — Python: smtplib.SMTPServerDisconnected: Connection unexpectedly closed (Email / SMTP)SMTPServerDisconnecte…Connectio…

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.