What IMAP is
IMAP (Internet Message Access Protocol; IMAP4rev1 in RFC 3501, IMAP4rev2 in RFC 9051) is used to read and organise mail that lives on the server. The client sees folders, messages and flags (read, answered, flagged) stored on the server, so the phone, the laptop and the webmail all see the same thing.
It is richer than POP3: the client can ask for just the headers or part of a message, search on the server, move messages between folders and get instant notifications with IDLE.
Tags and responses
| Prefix | What it is | Example |
|---|---|---|
| MJEB4 | Tag: the client puts it on each command and the server repeats it at the end | MJEB4 FETCH 1 (…) → MJEB4 OK Fetch completed |
| * | Untagged response: data or notifications from the server | * 1 EXISTS |
| + | Continuation: the server expects more from the client | + idling |
| OK, NO, BAD | Result: success, the operation failed, or the command is malformed | MJEB2 OK [READ-WRITE] |
Thanks to tags, a client can send several commands in a row and match each response with its command.
How it works: from the greeting to LOGOUT
Six steps: the connection, the login, the inbox, waiting with IDLE, reading and closing.
Connection to port 143 and greeting
The script opens TCP to port 143 on mx1. As in POP3, the server speaks first, with an untagged response (the asterisk): * OK and, in brackets, its capabilities: IMAP4rev1, IDLE, STARTTLS (it can encrypt the connection) and AUTH=PLAIN. If it did not allow logging in without TLS, it would announce LOGINDISABLED.
Tags, CAPABILITY and LOGIN
imaplib picks a four-letter tag (MJEB) and numbers each command: MJEB0, MJEB1… The final response repeats the tag: that is how the client knows which command it answers, and it can send several without waiting. MJEB0 CAPABILITY asks for the list again, and Dovecot warns there will be more after login. MJEB1 LOGIN sends the user name and the password in quotes, in clear text. The MJEB1 OK response carries the full capabilities: IDLE, MOVE, CONDSTORE, SPECIAL-USE and many more.
SELECT INBOX: the mailbox state
SELECT INBOX opens the inbox. The server answers with several untagged lines: the flags (\Seen, \Answered, \Flagged, \Deleted, \Draft), 0 EXISTS (it is empty: the POP3 script downloaded and deleted the previous alert) and two key numbers, UIDVALIDITY and UIDNEXT 2. Each message has a UID that never changes and is never reused: 1 was the deleted alert, so the next one will be 2. The tagged line closes with OK [READ-WRITE].
IDLE: the notification comes on its own
MJEB3 IDLE leaves the connection waiting and the server confirms with + idling. 72 seconds later, the Toronto link comes back, another R1 applet sends the restored alert and, as soon as it reaches the mailbox, Dovecot sends on its own * 1 EXISTS and * 1 RECENT: there is a new message. That is push email, with no polling every few minutes as a POP3 client would do. The script answers DONE (untagged) and the server closes the IDLE with MJEB3 OK.
FETCH and STORE: read and flag on the server
MJEB4 FETCH asks for the UID, the flags and just two headers of message 1. BODY.PEEK reads without marking the message as read (BODY, without PEEK, would mark it). The response carries UID 2 and the headers in a literal: {N} announces how many bytes follow, which are read as is. Then MJEB5 STORE 1 +FLAGS (\Seen) marks the message as read on the server: any other device that opens the mailbox will see it as read.
LOGOUT: the message stays on the server
MJEB6 LOGOUT closes the session: the server answers * BYE and MJEB6 OK. Unlike the POP3 lab, nothing was deleted: the alert is still on mx1, marked as read, and the NOC's phone or webmail will see it the same way. That is the IMAP model: the server keeps the mail, the folders and the flags, and the clients sync. To encrypt it all, the secure version uses IMAP4_SSL on port 993.
Sequence numbers and UIDs
| Sequence number | UID | |
|---|---|---|
| What it is | The message's position in the folder (1, 2, 3…) | A fixed message identifier |
| Does it change? | Yes: when a message is deleted, the next ones are renumbered | No, as long as UIDVALIDITY does not change |
| Is it reused? | Yes | Never: that is why UIDNEXT is 2 in this lab |
Syncing clients store UIDs and check UIDVALIDITY on every SELECT.
IMAP versus POP3
| IMAP | POP3 | |
|---|---|---|
| Where mail stays | On the server | On the device that downloaded it |
| New-mail notifications | Instant, with IDLE | The client polls from time to time |
| Folders and flags | Yes, on the server | No |
| Ports | 143, and 993 with TLS | 110, and 995 with TLS |
Secure IMAP
| Option | How it works | Port |
|---|---|---|
| IMAPS | TLS from the first byte (implicit TLS, recommended by RFC 8314) | TCP 993 |
| STARTTLS | Starts in clear text and the STARTTLS command turns the connection into TLS (RFC 2595) | TCP 143 |
| LOGINDISABLED | The server announces it when it does not allow LOGIN without TLS (Dovecot's default) | TCP 143 |
In Python, imaplib.IMAP4_SSL uses port 993. Give it ssl_context=ssl.create_default_context() so it verifies the certificate: without it, the connection is encrypted, but who is on the other end is not checked.
Frequently asked questions
Why BODY.PEEK and not BODY?
Because FETCH BODY[…] marks the message as read (\Seen) when it reads it. BODY.PEEK[…] reads it without changing the flags; the script then decides when to flag it with STORE.
How long can an IDLE last?
RFC 2177 asks the client to renew it before 29 minutes, because servers may close an idle connection after 30. That is why the script uses duration=29 * 60. Dovecot also sends "* OK Still here" every 2 minutes so firewalls and NAT do not close the connection.
What is \Recent?
A flag that only exists in the session that sees the message first. IMAP4rev2 (RFC 9051) removed it.
What happens if UIDVALIDITY changes?
The UIDs the client stored are no longer useful: it must discard its copy of that folder and sync it again. It happens, for example, if the folder is deleted and created again.
How do I see IMAP in Wireshark?
With the imap filter. In an unencrypted session, each line shows up as a request or a response, and the LOGIN password is visible as is.
What you take away
- IMAP keeps mail, folders and flags on the server: TCP 143, or 993 with TLS.
- Each command carries a tag; the final response repeats it with OK, NO or BAD.
- Responses with * are data or notifications from the server; + asks to continue.
- UIDs identify a message forever; UIDNEXT and UIDVALIDITY make syncing possible.
- IDLE turns email into push: the server notifies right away.
- BODY.PEEK reads without flagging; STORE changes flags on the server.
NEXT STEP
From this lesson to the CCNA 2.0 Course
Network services and their security are part of The House of Routing's CCNA 2.0 Course, with the mail and alerts of the Canadá Logística lab. We will let you know when it opens.
Upcoming protocol lessons
Sources
- RFC 3501 · Internet Message Access Protocol - Version 4rev1
- RFC 9051 · Internet Message Access Protocol (IMAP) - Version 4rev2
- RFC 2177 · IMAP4 IDLE command
- RFC 8314 · Cleartext Considered Obsolete: Use of TLS for Email Submission and Access
- Python 3.14 · imaplib — IMAP4 protocol client (idle)
- Dovecot 2.3 · SSL Configuration (ssl, disable_plaintext_auth)
Lesson prepared with AI assistance and checked against the sources cited. The bytes of every packet are generated according to the standard and verified with an independent tool.