Sub topic 9
Email ports: SMTP, IMAP and POP3
This chapter assumes you know what a port and a socket are, the difference between TCP and UDP, and how a client connects to a server. It also assumes you can run short Python scripts and terminal commands. It builds on the earlier chapter on plain-text protocols and their TLS variants.
Three protocols, two jobs
Email uses separate protocols for sending and for reading.
- SMTP (Simple Mail Transfer Protocol) moves a message from one server to another. A mail client also uses it to hand a new message to its own server.
- IMAP (Internet Message Access Protocol) lets a client read and manage mail that stays on the server.
- POP3 (Post Office Protocol version 3) lets a client download mail, usually to keep it locally.
All three run over TCP. Each is a text conversation of commands and numeric replies, so each needs the reliable, ordered byte stream that TCP provides.
The port table
| Protocol | Purpose | Port | Security |
|---|---|---|---|
| SMTP | Server-to-server relay | 25 | Plain text, with optional STARTTLS |
| SMTP submission | Client to its own server | 587 | STARTTLS, with authentication |
| SMTPS | Client to its own server | 465 | Implicit TLS |
| IMAP | Read mail on the server | 143 | Plain text, with optional STARTTLS |
| IMAPS | Read mail on the server | 993 | Implicit TLS |
| POP3 | Download mail | 110 | Plain text, with optional STLS |
| POP3S | Download mail | 995 | Implicit TLS |
Ports 25, 110 and 143 are well-known ports. Ports 465, 587, 993 and 995 are also in the IANA registry, but each came later as a separate assignment for a specific purpose.
How a message travels
A message crosses two kinds of hop. The first is submission, from the sender's client to the sender's own mail server. The second is relay, from server to server. Reading happens separately, between the recipient's client and the recipient's mailbox server.
The sender's server finds the recipient's server with a DNS MX (mail exchanger) record for the recipient's domain. It then connects to port 25 on that host.
Why submission and relay use different ports
Port 25 accepts mail from any other server, so it cannot require a login. Spammers abused it from infected home computers. Many ISPs and cloud providers therefore block outbound port 25 from customer networks.
Submission fixes this. Port 587 requires the client to authenticate, so the server only accepts mail from known users. Authenticated clients can then send mail even where port 25 is blocked.
- Run a mail server that receives internet mail: open inbound 25.
- Run a mail client or application that sends mail through a provider: use 587 or 465 outbound.
- Never configure an application to send through port 25 on a provider's server unless the provider says to. It is often blocked or unauthenticated.
STARTTLS versus implicit TLS
There are two ways to protect a mail connection.
- Implicit TLS: the client starts the TLS handshake immediately after the TCP connection opens. Ports 465, 993 and 995 work this way.
- STARTTLS: the connection starts in plain text on the standard port. The client asks the server to upgrade, and both sides then run the TLS handshake on the same connection. Ports 25, 587, 143 and 110 can do this. The POP3 command is named STLS.
A typical STARTTLS exchange on port 587 looks like this:
S: 220 mail.example.com ESMTP ready
C: EHLO client.example.org
S: 250-mail.example.com
S: 250-STARTTLS
S: 250 AUTH PLAIN LOGIN
C: STARTTLS
S: 220 Ready to start TLS
(TLS handshake, then EHLO is sent again over the encrypted channel)STARTTLS has a weakness. An attacker on the network path can remove the STARTTLS line from the server's reply. A client that treats TLS as optional then continues in plain text, which is a downgrade attack. Implicit TLS has no plain-text phase to strip. RFC 8314 recommends implicit TLS for mail submission and access, so 465 is again the preferred submission port in current guidance. Port 587 with mandatory STARTTLS remains widely supported.
IMAP versus POP3
The two reading protocols differ in where the mail lives.
| Feature | IMAP | POP3 |
|---|---|---|
| Mail stays on server | Yes | Usually deleted after download |
| Multiple devices | Folders and read flags stay in sync | Each device may see only what it downloaded |
| Server-side search and folders | Yes | No |
| Connection pattern | Long-lived, often idle | Short: connect, fetch, quit |
Choose IMAP unless you have a specific reason, such as a very small mailbox quota or a device that must hold the only copy of the mail. If you use POP3, check whether the client is set to leave a copy on the server. Otherwise a second device will find the mailbox empty.
Looking up the names and testing the greeting
Service names in the system services database vary between systems. Port 143 is often named imap2 on Linux, and smtps may be missing. The script below handles missing names instead of crashing.
import socket
names = ["smtp", "submission", "smtps", "imap", "imap2", "imaps", "pop3", "pop3s"]
for name in names:
try:
print(f"{name:10} {socket.getservbyname(name, 'tcp')}")
except OSError:
print(f"{name:10} not in the services database")The next script connects to a mail server and prints its first line. Every SMTP, IMAP and POP3 server speaks first, so you only need to read. Run it only against servers you own or are allowed to test. Pass tls as the third argument for the implicit-TLS ports.
import socket
import ssl
import sys
host = sys.argv[1]
port = int(sys.argv[2])
use_tls = len(sys.argv) > 3 and sys.argv[3] == "tls"
with socket.create_connection((host, port), timeout=10) as raw:
if use_tls:
context = ssl.create_default_context()
with context.wrap_socket(raw, server_hostname=host) as s:
print(s.version())
print(s.recv(512).decode("latin-1").strip())
else:
print(raw.recv(512).decode("latin-1").strip())Example runs, with mail.example.net standing in for your own server:
python3 greeting.py mail.example.net 25
python3 greeting.py mail.example.net 993 tlsA healthy SMTP server answers with a line starting 220. IMAP answers * OK, and POP3 answers +OK. If the connection times out, a firewall is probably dropping packets. If it is refused immediately, nothing is listening on that port.
For a quick reachability check from the shell, reuse the earlier tools:
ss -tulnp | grep -E ':(25|465|587|143|993|110|995)\b'
nc -vz mail.example.net 587Common errors
- Sending through port 25 from a home or cloud network. The connection times out because the provider blocks it. Switch to 587 or 465 with authentication.
- Mixing up the port and the security mode. A client set to implicit TLS on port 587 hangs or fails the handshake, because the server is waiting to speak plain text first. Match the mode to the port in the table.
- Certificate name mismatch. The client checks that the certificate matches the host name you typed, so connect by the name on the certificate, not by IP address.
- Opening only 993 on a server and leaving 143 closed, then finding that some clients cannot connect. Decide which ports you support, and document them.
- Forgetting that the firewall rule must allow the replies too. Stateful firewalls handle this, while stateless filters need the ephemeral port range as well.
Exercise
A cloud-hosted web application must send password-reset emails through your company's mail provider, and it cannot connect to the provider on port 25. Which port and security mode should you configure, and why does the failure happen on port 25?
Show answer
Use port 587 with mandatory STARTTLS, or port 465 with implicit TLS (preferred by RFC 8314), and supply login credentials. Port 25 is for unauthenticated server-to-server relay, and cloud providers commonly block outbound traffic on it to stop spam. Submission ports require authentication, so they are not blocked in the same way.
Exercise
A user reads email on a phone and a laptop. After setting up POP3 on the phone, the laptop shows an empty inbox. What is the likely cause, and which protocol and port should you move to?
Show answer
The phone's POP3 client downloaded the messages and deleted them from the server. Switch both devices to IMAP, using port 993 with implicit TLS, so that the mailbox and its read state stay on the server and stay in sync.