# Python: ConnectionRefusedError: [Errno 111] Connection refused — smtplib connect to localhost:25

**Code:** `ConnectionRefusedError [Errno 111] (SMTP)` · **Category:** Email / SMTP · **Seen on:** SMTP, Python, Linux

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.

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

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/email/python-smtp-connection-refused)
