POP3 · Post Office Protocol v3

Download the mail, then it’s yours.

11 · Application · TCP 110 / 995

POP3

Post Office Protocol v3

Download the mail, then it’s yours.

LearnProtocolTier 2 · MechanismsUnderstand10 minInfrastructure

You will be able to: trace a POP3 exchange and decode it byte by byte.

Prove it: Ray Serve Distributed Actor Deployment

System design›Protocols

Study with AI
ClaudeChatGPTGeminiPerplexityMore

Part 1 of 5

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.

Part 2 of 5

Question

What this lesson prepares you to answer. Keep these in mind while you watch the packets.

2.1Target questions

  1. Why does POP3 struggle with multiple devices?
  2. When are deletions applied?

The answers are in part 4.

Part 3 of 5

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.

Client
Server
+OK ready
USER / PASS
STAT, LIST
RETR 1
DELE 1, QUIT
Step 1 of 5

The server greets.

← BackPlayNext →

3.2Decode it byte by byte

Click any byte or row. Each colour is one field.

Session

POP3 on port 110 (995 with TLS) downloads mail. Server replies begin with +OK or -ERR.

00002b4f4b20504f50332073657276657220+OK POP3 server
001072656164790d0a5553455220626f620dready··USER bob·
00200a50415353202a2a2a2a2a2a2a2a0d0a·PASS ********··
0030535441540d0a2b4f4b20322035313230STAT··+OK 2 5120
00400d0a5245545220310d0a44454c452031··RETR 1··DELE 1
00500d0a515549540d0a··QUIT··
FieldGreeting
Length23 bytes
Text+OK POP3 server ready␍␊

From the server on connect.

FieldOffsetBytesHex
Greeting0232b 4f 4b 20 50 4f 50 33 …
USER231055 53 45 52 20 62 6f 62 0d 0a
PASS331550 41 53 53 20 2a 2a 2a …
STAT48653 54 41 54 0d 0a
STAT reply54122b 4f 4b 20 32 20 35 31 32 30 0d 0a
RETR66852 45 54 52 20 31 0d 0a
DELE74844 45 4c 45 20 31 0d 0a
QUIT82651 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.

pop3.tsTypeScriptCopy
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}
27 linesUTF-8TypeScript
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.

Part 4 of 5

Q&A

Check your understanding. Answer out loud, then open.

4.1Check yourself

Why does POP3 struggle with multiple devices?
It has no shared state such as flags or folders and often deletes mail after download.
When are deletions applied?
At QUIT, in the update state.
Part 5 of 5

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.
← SMTPMark as understoodIMAP →