Day 1 · How email works
IMAP, POP3 and JMAP: how mail apps read your mailbox
Course contents
SMTP only moves mail to a mailbox. Reading it is a separate job with separate protocols: POP3, the oldest and simplest; IMAP, which almost every mail app speaks; and HTTP-based APIs such as JMAP, Microsoft’s Exchange protocols and Graph, and the Gmail API. This lesson types IMAP and POP3 by hand, explains what the newer protocols do better, and covers the part that generates most help-desk tickets: how apps log in and find their settings.
POP3: download and (usually) delete
POP3 (RFC 1939) comes from the days of dial-up: connect, download everything, delete it from the server, disconnect. It knows one mailbox, no folders, and almost no state on the server.
openssl s_client -connect 127.0.0.1:995 -quiet
+OK Dovecot (Ubuntu) ready.
USER alice
+OK
PASS alice-lab-pass
+OK Logged in.
STAT
+OK 3 1468
LIST
+OK 3 messages:
1 489
2 497
3 482
.
QUIT
+OK Logging out.
STAT gives the number of messages and the total size, LIST the size of each, RETR n downloads one and DELE n marks it for deletion when you QUIT. That’s nearly the whole protocol. Replies are +OK or -ERR.
POP3 still has uses (an application that collects mail from a mailbox and processes it, an archive that pulls a copy of everything), but for people it’s the wrong choice: read status, folders and sent mail don’t sync between devices, and if the app is set to delete from the server, the phone and the laptop each get part of the mail.
IMAP: the mailbox stays on the server
IMAP (IMAP4rev1 in RFC 3501, IMAP4rev2 in RFC 9051) keeps all mail on the server and lets any number of clients work with it at the same time: folders, read and unread state, flags, search, and partial downloads (a phone can fetch just the headers, or one attachment). The server is the single source of truth, which is why your phone and your laptop agree.
IMAP commands start with a tag chosen by the client (a1, a2, …). The server can send any number of untagged lines starting with * and finishes with a tagged OK, NO or BAD, so the client can have several commands in flight and match each answer to its question.
Sequence numbers vs UIDs
Messages in a folder have two numbers. The sequence number is the position (1, 2, 3…) and changes whenever a message before it is removed: after the move above, message 2 became message 1. The UID is stable for as long as the folder’s UIDVALIDITY stays the same. Clients store UIDs to remember what they have already downloaded. If a server is migrated and UIDVALIDITY changes, every client throws away its cache and downloads the whole mailbox again, a classic cause of “Outlook is re-syncing everything” after a migration.
Deleting in IMAP
STORE +FLAGS (\Deleted) only marks a message; EXPUNGE removes marked messages. Most clients today move deleted mail to the Trash folder instead, and Gmail maps folders onto labels, so the same message can appear in several “folders” and deleting it from one only removes a label. When users say “I deleted it but it’s still there”, the client settings for deletion and expunge are the place to look.
Server-side rules: Sieve
Filters that run in the mail app only work while the app is open. Rules that should always run belong on the server. The standard language for that is Sieve (RFC 5228), supported by Dovecot (Pigeonhole), Fastmail, Proton and many others, and managed from clients over ManageSieve (RFC 5804):
require ["fileinto", "vacation"];
if header :contains "List-Id" "builds.example.com" {
fileinto "CI";
stop;
}
vacation :days 7 :subject "Out of office" "I'm away until Monday.";
Gmail filters and Microsoft 365 inbox rules do the same job in their own formats. As an admin, remember that rules can also forward mail outside the company, which attackers love; lesson 17 shows how to find those.
JMAP: email over HTTPS and JSON
IMAP is a powerful but awkward protocol: a custom text syntax, one connection per folder for push, and a lot of client state. JMAP (RFC 8620 for the core, RFC 8621 for mail, 2019) does the same job over HTTPS with JSON, batches many operations into one request, syncs efficiently with state strings, and uses one push channel for everything. It also covers sending, so a client needs only one protocol. Fastmail (whose engineers designed it), Stalwart, Cyrus and Apache James support it.
POST /jmap/api/ HTTP/1.1
Content-Type: application/json
{"using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
"methodCalls": [
["Email/query", {"accountId": "u1", "filter": {"inMailbox": "inbox-id"},
"sort": [{"property": "receivedAt", "isAscending": false}], "limit": 10}, "q"],
["Email/get", {"accountId": "u1", "#ids": {"resultOf": "q", "name": "Email/query", "path": "/ids"},
"properties": ["from", "subject", "receivedAt"]}, "g"]]}
One request finds the ten newest messages in the inbox and fetches their sender and subject, using the result of the first call in the second. With IMAP that would be several round trips.
Microsoft and Google protocols
The two biggest providers mostly speak their own protocols, and IMAP is a compatibility option:
| Protocol | Used by | Notes |
|---|---|---|
| MAPI over HTTP | Outlook for Windows (classic) with Exchange / Microsoft 365 | Full Exchange features: calendars, shared mailboxes, delegation |
| EWS (Exchange Web Services) | Older Mac clients, many third-party apps, migration tools | Microsoft is retiring EWS in Exchange Online in favor of Graph; check the current timeline before you build on it |
| Exchange ActiveSync (EAS) | Phone mail apps (iOS Mail, Android) | Mail, calendar and contacts sync for mobile devices |
| Microsoft Graph | New Outlook, apps and scripts | The REST API for all of Microsoft 365 |
| Gmail API | Gmail apps, scripts and integrations | REST API over Gmail’s labels and threads |
| IMAP / POP3 / SMTP | Any standard client | Both providers support them, with OAuth |
Logging in: passwords, app passwords and OAuth
A password typed into a mail app and stored on the device is a weak spot: it can’t do multi-factor authentication, and when it leaks it gives full access. The big providers have therefore turned off password-only (“basic”) authentication:
- Microsoft began turning off basic authentication for IMAP, POP3, EWS, ActiveSync and the other client protocols in Exchange Online on 1 October 2022. Basic authentication for SMTP AUTH (apps and devices that send through
smtp.office365.com) is on a separate, longer retirement schedule that Microsoft has moved more than once; check the current Microsoft 365 message center notice before planning around a date. - Google shut off “less secure app” access (plain passwords) for all Google accounts, Workspace included, on 14 March 2025. IMAP, POP3, SMTP, CalDAV and CardDAV now need OAuth, or an app password on accounts with 2-Step Verification.
With OAuth 2.0, the app opens the provider’s login page (with MFA), receives a token instead of the password, and then logs in to IMAP or SMTP with AUTH XOAUTH2 or AUTH OAUTHBEARER (RFC 7628). Modern versions of Outlook, Apple Mail, Thunderbird and the phone apps all do this. The tickets that remain come from old devices and scripts: multifunction printers that “scan to email”, alarm panels, old line-of-business apps. Fix them with OAuth support, an app password, or an SMTP relay that authenticates by IP (covered in lesson 7).
Autodiscover and autoconfig
“Enter your email address and password” works because the app looks up the server settings itself. There are several mechanisms, and a domain should support the ones its users’ apps use:
- Microsoft Autodiscover: Outlook queries
https://autodiscover.example.com/autodiscover/autodiscover.xmland related URLs, or follows a CNAMEautodiscover.example.com→autodiscover.outlook.comfor Microsoft 365. A broken autodiscover record is the classic cause of Outlook repeatedly asking for a password. - Thunderbird autoconfig:
https://autoconfig.example.com/mail/config-v1.1.xml, or a lookup in Thunderbird’s central database of big providers. - SRV records (RFC 6186 and RFC 8314): the domain advertises its servers in DNS. Fastmail publishes them, for example:
dig +short _imaps._tcp.fastmail.com SRV
dig +short _submissions._tcp.fastmail.com SRV
0 1 993 imap.fastmail.com.
0 1 465 smtp.fastmail.com.
Each answer is priority, weight, port and host: “IMAP over TLS is at imap.fastmail.com port 993; submission over TLS at smtp.fastmail.com port 465”.
- Apple configuration profiles (
.mobileconfig) and MDM push the settings to managed iPhones and Macs.
When nothing is discoverable, the manual settings any user needs are: IMAP server and port 993 with SSL/TLS, SMTP server and port 465 with SSL/TLS (or 587 with STARTTLS), user name (usually the full email address), and the authentication method.