POP3
Post Office Protocol v3
Download the mail, then it’s yours.
System design›Protocols
Basics
What the protocol is for, and the ideas it is built on.
1.1What it does
The protocol in plain words, before any bytes.
POP3 is the simplest way to read mail: log in, list messages, download them and usually delete them from the server. After that, the mail lives on one device.
It was designed for dial-up connections where you connected briefly and read offline. It is still used, but IMAP suits several devices better.
1.2Key ideas
The handful of ideas everything else follows from.
Three states
Authorisation, Transaction and Update.
Simple and stateless between sessions
No folders, no flags. Just a list of messages.
Leave on server
Clients can keep copies on the server for some days so other devices can fetch them too.
Question
What this lesson prepares you to answer. Keep these in mind while you watch the packets.
2.1Target questions
- Why does POP3 struggle with multiple devices?
- When are deletions applied?
The answers are in part 4.
Detailed answer
Watch a real exchange step by step, then take each message apart field by field.
3.1Follow the packets
Step through the exchange between the two sides.
The server greets.
3.2Decode it byte by byte
Click any byte or row. Each colour is one field.
POP3 on port 110 (995 with TLS) downloads mail. Server replies begin with +OK or -ERR.
From the server on connect.
| Field | Offset | Bytes | Hex | |
|---|---|---|---|---|
| Greeting | 0 | 23 | 2b 4f 4b 20 50 4f 50 33 … | |
| USER | 23 | 10 | 55 53 45 52 20 62 6f 62 0d 0a | |
| PASS | 33 | 15 | 50 41 53 53 20 2a 2a 2a … | |
| STAT | 48 | 6 | 53 54 41 54 0d 0a | |
| STAT reply | 54 | 12 | 2b 4f 4b 20 32 20 35 31 32 30 0d 0a | |
| RETR | 66 | 8 | 52 45 54 52 20 31 0d 0a | |
| DELE | 74 | 8 | 44 45 4c 45 20 31 0d 0a | |
| QUIT | 82 | 6 | 51 55 49 54 0d 0a |
3.3Decode it in code
A small decoder, tested against the exact bytes shown above.
Reading POP3
It's one of the simplest protocols on the internet, and its delete rule is worth copying.
1/** POP3 replies start with +OK or -ERR. */2export function parsePop3(line: string) {3 const ok = line.startsWith("+OK");4 return { ok, text: line.replace(/^(\+OK|-ERR)\s*/, "").trimEnd() };5}6 7/** STAT answers with a message count and total size. */8export function parseStat(line: string): { messages: number; bytes: number } {9 const { ok, text } = parsePop3(line);10 if (!ok) throw new Error(text);11 const [messages, bytes] = text.split(" ").map(Number);12 return { messages, bytes };13}14 15/** Deletes take effect only at QUIT, so a dropped connection deletes nothing. */16export class Mailbox {17 private marked = new Set<number>();18 private messages: Set<number>;19 constructor(ids: number[]) { this.messages = new Set(ids); }20 dele(id: number): void { if (this.messages.has(id)) this.marked.add(id); }21 rset(): void { this.marked.clear(); }22 quit(): number[] {23 for (const id of this.marked) this.messages.delete(id);24 this.marked.clear();25 return [...this.messages];26 }27}line.startsWith("+OK")- Only two possible statuses.
quit()- Deletes apply only at QUIT, so a dropped connection deletes nothing.
Tested by npm run test:modules.
Q&A
Check your understanding. Answer out loud, then open.
4.1Check yourself
Why does POP3 struggle with multiple devices?
When are deletions applied?
Extra depth
The mistakes people make with this protocol in real systems.
5.1Watch out for
- Plain POP3 exposes the password. Use port 995.
- Deleting after download means a second device sees nothing.
- No sync of read or unread status.