Sub topic 9
Email Ports: SMTP, IMAP and POP3
This chapter assumes you know what a port is, how TCP carries a connection to a port, and how a plain-text protocol differs from its TLS-protected variant. Email uses three protocols, each with its own ports and its own job.
Three protocols, three jobs
Email separates sending from reading.
- SMTP (Simple Mail Transfer Protocol) moves messages. A mail client uses it to hand a message to a server, and servers use it to pass messages to each other.
- IMAP (Internet Message Access Protocol) lets a client read mail that stays on the server. Folders and read flags are shared across all your devices.
- POP3 (Post Office Protocol version 3) downloads mail to the client. By default the server then deletes its copy.
All three run over TCP, because a lost byte in a message or a login exchange is not acceptable.
The port table
Each protocol has a plain-text port and a TLS port. Submission is a third SMTP role, described below.
| Protocol | Plain-text port | Encrypted port | Notes |
|---|---|---|---|
| SMTP (server to server) | 25 | none standard | Relay between mail servers |
| SMTP submission | 587 | 465 | 587 upgrades with STARTTLS. 465 is TLS from the first byte |
| IMAP | 143 | 993 | 143 upgrades with STARTTLS. 993 is implicit TLS |
| POP3 | 110 | 995 | 110 upgrades with STARTTLS. 995 is implicit TLS |
Two ways to add TLS
- Implicit TLS means the client starts the TLS handshake as soon as the TCP connection opens. Ports 465, 993 and 995 work this way.
- STARTTLS means the client connects in plain text, asks the server to switch to TLS, and then performs the handshake on the same port. Ports 587, 143 and 110 work this way.
Current guidance prefers implicit TLS for client connections, because a STARTTLS session can be forced to stay in plain text by an attacker who strips the upgrade offer, unless the client refuses to continue without it.
Why SMTP uses two ports
Port 25 is for server-to-server relay. Many residential and cloud networks block outbound port 25 to limit spam from infected machines. Mail clients therefore submit outgoing messages on 587 or 465, where the server requires authentication before it accepts mail.
Looking at the exchange
SMTP and IMAP greet you with a text banner, so you can see this on a server you own or run. The script below connects to a port and prints the greeting. Replace the host and port with your own mail server.
import socket
host, port = "mail.example.com", 143
with socket.create_connection((host, port), timeout=5) as s:
print(s.recv(1024).decode("latin-1").strip())On an IMAP server, the greeting begins with * OK. An SMTP server on port 587 begins with 220. Ports 465, 993 and 995 will not show a readable greeting, because the server expects a TLS handshake first.
Exercise
A mail client is set to IMAP on port 993 with "STARTTLS" selected, and the connection fails. What is wrong, and what are two ways to fix it?
Show answer
Port 993 expects implicit TLS, so the client must start the TLS handshake immediately, but STARTTLS makes it send plain-text commands first. Either change the security setting to SSL/TLS (implicit) and keep port 993, or switch to port 143 and keep STARTTLS.