LABS · PROTOCOLS · CCNA V2.0

IMAP: mail stays on the server

The protocol to read mail from several devices, line by line: the tag on each command, the mailbox state, instant notification with IDLE, reading without flagging and flags kept on the server.

Level: intermediate · 15 minutes

Interactive simulation

The animation needs JavaScript. The full lesson is written out below.

Download the capture and open it in Wireshark to keep exploring.

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

PrefixWhat it isExample
MJEB4Tag: the client puts it on each command and the server repeats it at the endMJEB4 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, BADResult: success, the operation failed, or the command is malformedMJEB2 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.

  1. 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.

  2. 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.

  3. 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].

  4. 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.

  5. 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.

  6. 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 numberUID
What it isThe 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 renumberedNo, as long as UIDVALIDITY does not change
Is it reused?YesNever: that is why UIDNEXT is 2 in this lab

Syncing clients store UIDs and check UIDVALIDITY on every SELECT.

IMAP versus POP3

IMAPPOP3
Where mail staysOn the serverOn the device that downloaded it
New-mail notificationsInstant, with IDLEThe client polls from time to time
Folders and flagsYes, on the serverNo
Ports143, and 993 with TLS110, and 995 with TLS

Secure IMAP

OptionHow it worksPort
IMAPSTLS from the first byte (implicit TLS, recommended by RFC 8314)TCP 993
STARTTLSStarts in clear text and the STARTTLS command turns the connection into TLS (RFC 2595)TCP 143
LOGINDISABLEDThe 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

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.