📬

IMAP Protocol

Internet Message Access Protocol  ·  RFC 3501  ·  Interactive Learning Module

PORT 143 IMAPS: 993 Application Layer RFC 3501 Server-Side Storage
What is IMAP?

Internet Message Access Protocol — the standard for reading email while keeping messages on the server.

143
Standard Port
993
IMAPS (TLS/SSL)
3501
Defining RFC
1988
Year Introduced
IMAP Architecture Flow
📱 Email ClientThunderbird / Outlook
IMAP cmds (143)
🖥 IMAP Servermail.server.com
Responses
☁ Mail StoreMessages stay here
SMTP delivery
✉ MTAMail Transfer Agent
ℹ️
Key Concept: Unlike POP3 which downloads and often deletes messages, IMAP leaves all messages on the server. Your client merely views and manages them remotely, enabling multi-device access to the same mailbox.

🔵 IMAP Advantages

  • Messages stored on server — access from any device
  • Folder management on the server
  • Supports partial message fetch (just headers)
  • Flags: Seen, Answered, Flagged, Deleted, Draft
  • Server-side search capabilities
  • Multiple simultaneous connections

🔴 IMAP Considerations

  • Requires persistent internet connection
  • Consumes server storage space
  • More complex protocol than POP3
  • Port 143 (unencrypted) — always use port 993
  • Higher server resource usage
  • Misconfigured IDLE can drain battery

IMAP Message Flags

IMAP tracks message state using server-side flags. These persist across all devices.

\\Seen
\\Answered
\\Flagged
\\Deleted
\\Draft
\\Recent
IMAP Commands

IMAP uses tagged commands — each command is prefixed with a unique tag (A001, A002…) so responses can be matched to requests.

🏷️
Tagged Commands: Every IMAP command begins with a client-generated tag (e.g., A001 LOGIN user@example.com mypassword). The server response includes the same tag to confirm completion.
Command State Required Description Example Response
CAPABILITYAnyList server capabilities and extensions supported* CAPABILITY IMAP4rev1 STARTTLS AUTH=PLAIN
STARTTLSNot AuthUpgrade connection to TLS encryptionA001 OK Begin TLS negotiation
LOGINNot AuthAuthenticate with username and passwordA001 OK LOGIN completed
AUTHENTICATENot AuthAuthenticate via SASL mechanism (e.g., OAUTH2)A001 OK AUTHENTICATE completed
SELECTAuthenticatedOpen a mailbox for read/write access* 12 EXISTS  * 2 RECENT
EXAMINEAuthenticatedOpen a mailbox for read-only accessA002 OK [READ-ONLY] EXAMINE completed
LISTAuthenticatedList available mailboxes / folders* LIST (\HasNoChildren) "." INBOX
LSUBAuthenticatedList subscribed mailboxes* LSUB () "." "INBOX.Sent"
CREATEAuthenticatedCreate a new mailbox/folder on the serverA003 OK CREATE completed
DELETEAuthenticatedDelete an existing mailboxA004 OK DELETE completed
RENAMEAuthenticatedRename a mailboxA005 OK RENAME completed
STATUSAuthenticatedQuery mailbox status (messages, unseen count)* STATUS INBOX (MESSAGES 12 UNSEEN 3)
FETCHSelectedRetrieve message data (headers, body, flags)* 1 FETCH (FLAGS (\Seen) BODY[TEXT] {42})
SEARCHSelectedSearch messages matching criteria on the server* SEARCH 2 5 9
STORESelectedModify message flags (e.g., mark as read)* 1 FETCH (FLAGS (\Seen \Flagged))
COPYSelectedCopy messages to another mailboxA006 OK COPY completed
MOVESelectedMove messages to another mailbox (RFC 6851)A007 OK MOVE completed
EXPUNGESelectedPermanently remove messages marked \\Deleted* 3 EXPUNGE  A008 OK EXPUNGE completed
IDLESelectedServer push — get notified of new mail without polling+ idling  (until "DONE" sent)
NOOPAnyNo operation — keep connection alive, get updatesA009 OK NOOP completed
CLOSESelectedClose mailbox, silently expunge \\Deleted messagesA010 OK CLOSE completed
LOGOUTAnyEnd IMAP session and disconnect from server* BYE IMAP4rev1 Server logging out  A011 OK LOGOUT completed
⚠️
Security Note: Never use plain LOGIN over port 143 in production — always enforce STARTTLS or use IMAPS on port 993. Plain-text credentials are trivially captured by network sniffers.
Protocol Simulator

Step through an IMAP session interactively. Blue = server responses. Red = client commands.

imap-session — mail.example.com:143
# Select a scenario above to begin IMAP simulation
# Blue = server    Red = client commands
Connection States

IMAP defines four distinct connection states. Commands are only valid in certain states. Click each state to learn more.

Not Authenticated
Initial state after TCP connection. Client must authenticate before accessing mail.
Valid commands: CAPABILITY, NOOP, AUTHENTICATE, LOGIN, STARTTLS, LOGOUT

Entry point: Server sends greeting:
* OK IMAP4rev1 Service Ready

Exit: Successful LOGIN or AUTHENTICATE → Authenticated state
Authenticated
User is logged in. Can manage mailboxes but has not opened one yet.
Valid commands: All Not-Auth commands plus SELECT, EXAMINE, CREATE, DELETE, RENAME, LIST, LSUB, STATUS, SUBSCRIBE, UNSUBSCRIBE, APPEND

Exit: SELECT or EXAMINE → Selected state  |  LOGOUT → Logout state
Selected
A mailbox is open. Full access to messages — read, write, search, fetch.
Valid commands: All Authenticated commands plus CHECK, CLOSE, EXPUNGE, SEARCH, FETCH, STORE, COPY, MOVE, IDLE, UID commands

Server sends unilateral responses: EXISTS, RECENT, EXPUNGE, FETCH

Exit: SELECT/EXAMINE → new Selected  |  CLOSE → Authenticated  |  LOGOUT → Logout
Logout
Session is terminating. Server sends BYE and closes TCP connection.
Trigger: LOGOUT command, server-side timeout, or network error

Server response:
* BYE IMAP4rev1 Server logging out
A011 OK LOGOUT completed


Client must: Close the TCP connection after receiving BYE

State Transition Diagram

[TCP Connect]
    ↓ Server greeting: * OK IMAP4rev1 Ready
NOT AUTHENTICATED ──(LOGIN / AUTHENTICATE)──→ AUTHENTICATED
                                                  ↓ SELECT / EXAMINE
                                             SELECTED ←→ FETCH/STORE/SEARCH
                                                  ↓ LOGOUT (from any state)
                                             LOGOUT → TCP Close
Mailbox Explorer

Simulate IMAP's server-side folder management. Messages stay on the server — this is the key difference from POP3.

Folders
📥 INBOX 3
📤 Sent
📝 Drafts 1
🚫 Spam 7
🗑 Trash
💼 Work 2
🗄 Archive
IMAP: A001 SELECT INBOX
💡 Messages remain on the server. IMAP just shows you a view — your phone and laptop see the same mailbox.

What IMAP does behind the scenes

* OK IMAP4rev1 Service Ready
A001 LOGIN user@mvcc.edu ••••••••
A001 OK LOGIN completed
A002 SELECT INBOX
* 12 EXISTS  * 3 RECENT  * OK [UNSEEN 10]
A002 OK [READ-WRITE] SELECT completed
IMAP vs POP3

Understanding when to use each protocol is fundamental to email system design.

Feature IMAP (Port 143/993) POP3 (Port 110/995)
Storage LocationServer-side — alwaysDownloaded to client (usually deleted from server)
Multi-device Access✔ All devices see same mailbox✘ Downloaded messages only on one device
Folder ManagementServer-side folders synced across devicesLocal folders only (client-side)
Offline AccessRequires sync / cache setupFull offline access after download
Server Storage RequiredHigh — all messages stored indefinitelyLow — messages deleted after download
Protocol ComplexityComplex — 22+ commands, 4 states, tagsSimple — ~12 commands, 2 states
Bandwidth UsageFetch only what you need (headers first)Downloads entire messages always
Message Flags / StateRich flags: Seen, Flagged, Answered, DraftNo flag support
Server-Side Search✔ SEARCH command on server✘ Client must download all to search
Push Notifications✔ IDLE command for instant new mail✘ Must poll (check periodically)
Partial Message Fetch✔ Fetch headers, body sections separately✘ Must download complete message
RFC StandardRFC 3501 (IMAP4rev1), RFC 9051 (IMAP4rev2)RFC 1939
Best Use CaseMost modern email — multiple devicesSingle-device, limited server storage
💡
Rule of thumb: IMAP is almost always the right choice for modern email deployments. POP3 may still be appropriate in bandwidth-constrained environments or where server storage is severely limited.
Knowledge Check

Test your understanding of the IMAP protocol. Click an answer to receive immediate feedback.

0/10

Complete all questions to see your score.