The idea: push with SMTP, pull with IMAP or POP3

An email crosses several machines, and two kinds of protocol move it. SMTP (Simple Mail Transfer Protocol) pushes the message forward: from Alice's mail app to her provider's server, and from that server to the receiver's server. There it is saved in a mailbox. Later Bob's mail app pulls it out with IMAP or POP3. The two mail apps never talk to each other.

Six boxes left to right: Alice's app, smtp.sender.example, DNS resolver, mx.receiver.example, mail.receiver.example (Dovecot) and Bob's app. Arrows: SMTP submission on port 587 with AUTH, a DNS MX query, SMTP relay on port 25, LMTP into the mailbox, and IMAP 993 or POP3 995 from Bob's app pulling the mail.
SMTP pushes the mail to the receiver's server in two hops; Bob's app pulls it from the mailbox with IMAP or POP3.

The canvas follows one message, Lunch on Friday?, from alice@sender.example to bob@receiver.example. Each command and each reply is a box that flies from one machine to the other, and the panel below writes the session down line by line, as a mail server's log would (C: client, S: server). The journey has three stages; Next stage runs the next one.

Submission on port 587: STARTTLS, then AUTH

Alice's app does not deliver mail itself. It submits it to her provider's server, smtp.sender.example, on port 587, the submission port (RFC 6409). The server speaks first (220). The app says EHLO and the server lists its extensions. STARTTLS turns the same TCP connection into a TLS connection; after that the app must say EHLO again, and only now is AUTH offered. AUTH PLAIN sends the user name and password in base64, which is an encoding, not encryption: TLS is what keeps it secret. Port 587 relays only for users who logged in, so strangers cannot use the server to send spam (Demo 1: submit on port 587).

Many providers also accept submission on port 465, where TLS starts with the first byte instead of after STARTTLS (RFC 8314 now prefers it). The SMTP commands inside are the same.

Finding the receiver: the MX record

smtp.sender.example puts the message in its mail queue and then needs to know where mail for receiver.example goes. It asks DNS for the MX record (mail exchanger) of the domain after the @. The answer names a host and a preference: MX 10 mx.receiver.example. With several MX records, the server tries the lowest number first and the others as backups. It then looks up that host's address (A or AAAA record) and connects to it on port 25 (Demo 2: MX lookup, SMTP relay on port 25). DNS Resolution shows how the resolver finds such an answer.

Alice's app, smtp.sender.example, the DNS resolver and mx.receiver.example, time going down. Submission on port 587: the app sends EHLO, STARTTLS, EHLO again and AUTH PLAIN; the server answers 235 Authentication successful; the app sends MAIL FROM, RCPT TO, DATA and the message ending with a dot; the server answers 250 Ok: queued as 4F2A1C. Relay on port 25: smtp.sender.example asks DNS for the MX of receiver.example and gets MX 10 mx.receiver.example; it sends EHLO, STARTTLS and EHLO without AUTH, then MAIL FROM, RCPT TO, DATA and the message; the MX answers 250 Ok: queued as 7D3E9B and hands the mail with LMTP to bob's INBOX as UID 1.
Two SMTP hops, two queues: each server's 250 queued means it now owns the message.

The SMTP conversation and its reply codes

Server to server, the talk is the same as on submission, without AUTH: EHLO, STARTTLS, EHLO again, then one mail transaction: MAIL FROM, one RCPT TO per recipient, DATA, the message ending with a line that holds a single dot, and QUIT. Every command gets a three-digit reply. The first digit says what to do next:

ReplyMeaningOn the canvas
2xxdone, OK220 ready, 250 OK, 235 logged in, 221 bye
3xxgo on, send more354 send the message, end with "."
4xxtemporary failure: try again later(not shown) e.g. 451, 452 mailbox full
5xxpermanent failure: do not retry550 5.1.1 user unknown

The extra 2.1.5 or 5.1.1 after the code is an enhanced status code (RFC 3463): class, subject, detail. A dash after the code (250-SIZE) means more lines of the same reply follow. Between servers, STARTTLS is opportunistic: if the MX did not offer it, most senders would go on in plain text. MTA-STS and DANE are ways for a domain to demand TLS.

Envelope vs headers

MAIL FROM and RCPT TO are the envelope: servers route by them, and a bounce goes to the MAIL FROM address. The From:, To: and Subject: lines after DATA are headers, part of the message, and they are what Bob's app shows. The two do not have to match, which is why a Bcc works, and also why anyone can write a fake From:. SPF, DKIM and DMARC shows how the receiver checks who really sent a message. Every relay adds a Received: header at the top, so a message carries the trace of its hops.

550 and the bounce

With the typo address bbo@receiver.example, submission still works: smtp.sender.example cannot know which mailboxes exist at another domain. The MX knows, and answers RCPT TO with 550 5.1.1 User unknown. By then the sender's server has already promised to deliver the message (it said 250 queued to Alice), so it must report the failure. It writes a bounce, a delivery status notification (RFC 3464) from MAILER-DAEMON, to the envelope sender alice@sender.example (Demo 3: wrong address). With a 4xx reply it would keep the message in the queue and retry for several days before bouncing.

Delivery to the mailbox

SMTP ends at the receiver's server; it does not push the mail to Bob's laptop, which may be off. Here the MX (Postfix) hands the message to the mailbox store (Dovecot) with LMTP (RFC 2033), a cut-down SMTP for the last local hop. Dovecot saves it in bob's INBOX with a UID, a number that never changes, and no \Seen flag yet.

IMAP vs POP3

IMAP (port 993 with TLS) works on the mailbox where it is. Each command has a tag (a1, a2 …) and its final answer repeats the tag with OK, NO or BAD; lines starting with * carry data. LOGIN logs in, SELECT INBOX opens a folder and reports how many messages EXISTS, FETCH reads a message and sets \Seen on the server. IDLE (RFC 2177) keeps the connection open so the server can push * 2 EXISTS the moment new mail arrives (Demo 4: Bob reads with IMAP). Because messages, folders and flags all stay on the server, Bob's phone sees the same INBOX, already marked read.

POP3 (port 995 with TLS) is older and simpler: USER, PASS, RETR to download, DELE to mark for deletion, QUIT. Marked messages are deleted only at QUIT (the UPDATE state). The usual setup downloads and deletes, so the only copy ends up on one device and the phone finds 0 messages (Demo 5: POP3 downloads and deletes).

Left, IMAP: the INBOX stays on the server with two messages marked Seen, and both the laptop and the phone show the same two messages. Right, POP3: the server INBOX is empty after RETR, DELE and QUIT, the laptop holds the only copy, and the phone shows nothing.
IMAP keeps the mailbox on the server and every device shows it; POP3 moves the mail to the one device that downloaded it.
IMAPPOP3
Port (TLS)993 (143 + STARTTLS)995 (110 + STLS)
Mail stays on the serveryesusually deleted after download
Folders and flagson the server, shared by all devicesINBOX only, no flags
New mailpushed with IDLEthe app polls
Repliestagged OK / NO / BAD+OK / -ERR

See also SPF, DKIM and DMARC for how the MX decides whether alice@sender.example really sent this mail, the HTTPS connection for the TLS handshake that STARTTLS starts, and the TCP handshake under every connection on this page.

What the page leaves out

The TLS handshake itself, the full EHLO extension lists, PIPELINING, SIZE checks and 8BITMIME, MIME parts and attachments, several recipients and several MX hosts with fallback, 4xx retries and the queue's retry schedule, greylisting, SPF/DKIM/DMARC checks and spam filtering at the MX, MTA-STS and DANE, IMAP's UID commands, SEARCH, folders other than INBOX and the sequence-number shift after EXPUNGE, POP3's UIDL and "leave on server", and OAuth2 logins (XOAUTH2) that big providers use instead of passwords.

On the mind map

Open the Networking mind mapNetworkingLayers & links: frames on one local networkLayers & linksIP & routing: packets hop between networksIP & routingCore services: address, name and timeCore servicesTCP, UDP & sockets: end to end between two portsTCP, UDP & socketsHTTP & TLS: what a browser does per requestHTTP & TLSAPIs & push: how apps talk to serversAPIs & pushProxies & edge: boxes in the middle of the pathProxies & edgeContainers: virtual networks from Linux partsContainersReal-time media: voice and video over UDPReal-time mediaSecurity at the edge: filter traffic at the borderSecurity at the edgeDHCPDHCPDISCOVER, OFFER, REQUEST, ACK; renew at T1SNMP MIB and trapsSNMP MIB and trapsPoll to ask, trap to be told at onceNetFlow / IPFIXNetFlow / IPFIXFlow = key; export on FIN or timeoutDNS resolutionDNS resolutionroot, TLD, authoritative; cached for TTLDNS over HTTPS / TLSDNS over HTTPS / TLSHides names from the path, not the resolverSMTP, IMAP and POP3SMTP pushes to the MX; IMAP/POP3 pullDNSSEC chain of trustDNSSEC chain of trustparent's DS vouches for the child's KSKNTPNTP4 timestamps give clock offset and delayGPS TrilaterationGPS TrilaterationClock error is one more unknownOpen the Networking mind mapNetworkingaddress, name and timeCore servicesDHCPDHCPDISCOVER, OFFER, REQUEST, ACK; renew at T1SNMP MIB and trapsSNMP MIB and trapsPoll to ask, trap to be told at onceNetFlow / IPFIXNetFlow / IPFIXFlow = key; export on FIN or timeoutDNS resolutionDNS resolutionroot, TLD, authoritative; cached for TTLDNS over HTTPS / TLSDNS over HTTPS / TLSHides names from the path, not the resolverSMTP, IMAP and POP3SMTP pushes to the MX; IMAP/POP3 pullDNSSEC chain of trustDNSSEC chain of trustparent's DS vouches for the child's KSKNTPNTP4 timestamps give clock offset and delayGPS TrilaterationGPS TrilaterationClock error is one more unknown
This page on the Networking mind map · 83 pages in 10 branches · open it to see the whole map and test yourself in recall mode.