1. Library
  2. Computer Networks
  3. Email Protocols
  4. Protocols

Updated 8 hours ago

Before IMAP, email had a problem: your messages could only really live in one place. Download them to your laptop, and your phone couldn't see them. Read something on your phone, and your laptop wouldn't know. Every device had its own version of your mailbox, and keeping them in sync was your problem.

IMAP flipped this around. Instead of downloading messages to your devices, IMAP keeps them on the server. Your devices don't hold copies of your mailbox—they're windows into it. Read an email on your phone, and it's marked as read everywhere. Delete something on your laptop, and it vanishes from your tablet. The server is the truth; your devices just show you that truth.

The Conversation Between Client and Server

IMAP is a dialogue. Your email client talks to the server, asking questions and giving instructions. The server responds with information and confirmations.

Connecting and Logging In

The client connects (typically port 993 for encrypted connections) and authenticates:

S: * OK IMAP4rev1 Server Ready
C: A001 LOGIN username password
S: A001 OK LOGIN completed

Every client command starts with a tag (A001, A002, etc.) so responses can be matched to requests.

Opening a Mailbox

To work with messages, the client selects a folder:

C: A002 SELECT INBOX
S: * 172 EXISTS
S: * 5 RECENT
S: * OK [UNSEEN 12] First unseen message
S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
S: A002 OK [READ-WRITE] SELECT completed

The server immediately tells you what's inside: 172 messages total, 5 arrived recently, message 12 is the first you haven't read. This is why your email client can show you an unread count before downloading anything.

Fetching Messages

The client can ask for exactly what it needs:

C: A003 FETCH 1:5 (FLAGS ENVELOPE)
S: * 1 FETCH (FLAGS (\Seen) ENVELOPE (...))
S: * 2 FETCH (FLAGS () ENVELOPE (...))
S: * 3 FETCH (FLAGS (\Seen \Flagged) ENVELOPE (...))
S: A003 OK FETCH completed

This retrieves just the flags and envelope (sender, subject, date) for messages 1 through 5—enough to display a message list without downloading full content.

Two Ways to Point at a Message

IMAP has two numbering systems for messages, and understanding why reveals something about the problem it's solving.

Sequence numbers are simple: message 1 is the first in the folder, message 2 is the second, and so on. But delete message 5, and what was message 6 becomes message 5. Every message after it shifts down. Sequence numbers are convenient for "give me the first 10 messages" but terrible for "remember this specific message."

UIDs (Unique Identifiers) never change. A message assigned UID 47293 keeps that UID forever, even if thousands of other messages are deleted around it. UIDs only go up—newer messages always have higher UIDs than older ones.

Clients use sequence numbers for quick operations within a session. They use UIDs to track specific messages across sessions. Your email client remembers "I was looking at UID 47293," not "I was looking at message 5."

Flags: How State Syncs Across Devices

Flags are how IMAP tracks what you've done with messages:

  • \Seen — You've read it
  • \Answered — You've replied
  • \Flagged — You've starred or flagged it
  • \Deleted — Marked for deletion
  • \Draft — It's a work in progress

When you star an email on your phone, your client tells the server:

C: A004 STORE 47 +FLAGS (\Flagged)
S: * 47 FETCH (FLAGS (\Seen \Flagged))
S: A004 OK STORE completed

Now that flag exists on the server. When your laptop checks in, it sees the flag and shows you the star. The server is the truth; your devices reflect it.

The Deletion Dance

Deleting in IMAP is a two-step process. First you mark messages as deleted:

C: A005 STORE 23 +FLAGS (\Deleted)

But they're still there—just marked. To actually remove them, you expunge:

C: A006 EXPUNGE
S: * 23 EXPUNGE
S: A006 OK EXPUNGE completed

This is where the "Trash" metaphor in email clients comes from. Moving to trash marks as deleted. Emptying trash expunges.

Fetching Only What You Need

IMAP doesn't force you to download entire messages. This matters when you're on a slow connection or when messages have large attachments.

Headers only:

C: A007 FETCH 10 BODY[HEADER]

Enough to display who sent it and the subject line.

Just the text part:

C: A008 FETCH 10 BODY[1]

For a multipart message, grab just the text without the 50MB attachment.

A preview:

C: A009 FETCH 10 BODY[TEXT]<0.1000>

The first 1000 bytes—enough to show a snippet.

This selective fetching is why your email app can show you a list of hundreds of messages almost instantly, then load full content only when you tap on one.

Searching Without Downloading

IMAP can search on the server:

C: A010 SEARCH FROM "boss@company.com" SUBJECT "urgent"
S: * SEARCH 23 89 156
S: A010 OK SEARCH completed

The server does the work and returns just the matching message numbers. Your client didn't have to download every message to find these three. For a mailbox with 50,000 messages, this is the difference between a one-second search and a 20-minute download.

Real-Time Updates with IDLE

Without IDLE, email clients poll: check for new messages every few minutes. This wastes battery, wastes bandwidth, and means delays before you see new mail.

IMAP IDLE lets the client say "I'll wait here—tell me when something changes":

C: A011 IDLE
S: + idling
[time passes...]
S: * 173 EXISTS
C: DONE
S: A011 OK IDLE terminated

The server pushes a notification the moment a new message arrives. The client can then fetch it immediately. This is how your phone buzzes within seconds of receiving an email.

Folders and Hierarchy

IMAP organizes messages into folders (the protocol calls them mailboxes, but everyone calls them folders). INBOX is standard; everything else is up to you:

C: A012 LIST "" "*"
S: * LIST (\HasNoChildren) "/" INBOX
S: * LIST (\HasChildren) "/" Work
S: * LIST (\HasNoChildren) "/" Work/Projects
S: * LIST (\HasNoChildren) "/" Personal
S: A012 OK LIST completed

Create a folder on your laptop, and it appears on your phone. The folder structure lives on the server, just like the messages.

Security

IMAP connections should always be encrypted. Two approaches:

Implicit TLS (port 993): The connection is encrypted from the first byte, like HTTPS.

STARTTLS (port 143): Connect unencrypted, then upgrade:

C: A013 STARTTLS
S: A013 OK Begin TLS negotiation
[encryption begins]

Modern servers refuse to accept passwords until encryption is active.

Beyond passwords, IMAP supports OAuth2 (used by Gmail and Office 365), where your email client gets a token instead of storing your password directly.

Synchronization and Offline

Modern email clients cache messages locally. This lets you:

  • Read messages without a connection
  • Queue actions (delete, flag, move) while offline
  • Sync changes when connectivity returns

The synchronization challenge: what if you flag a message on your phone while offline, and delete it on your laptop? Clients handle this differently—some use last-write-wins, some prompt you, some try to merge. The protocol doesn't dictate a solution; it just provides the building blocks.

Extensions That Matter

IMAP's core is decades old, but extensions keep it current:

  • SORT — Server sorts messages so your client doesn't have to
  • THREAD — Server groups conversations
  • QUOTA — Query how much storage you've used
  • QRESYNC — Efficiently sync only what's changed since last time
  • COMPRESS — Compress the connection, saving bandwidth

Clients discover what's available:

C: A014 CAPABILITY
S: * CAPABILITY IMAP4rev1 IDLE SORT THREAD=REFERENCES QUOTA
S: A014 OK CAPABILITY completed

Common Frustrations

Large mailboxes slow down. With hundreds of thousands of messages, operations take time. Archiving old mail or splitting into subfolders helps.

Connection limits. Servers cap concurrent connections per account. Multiple devices plus multiple apps can hit this ceiling.

Storage quotas. Since messages stay on the server, you can run out of space. Your client might tell you; your server administrator definitely will.

Folder delimiter confusion. Some servers use / between folder levels, some use .. This occasionally causes folder sync issues between different clients.

Frequently Asked Questions About IMAP

Was this page helpful?

😔
🤨
😃
IMAP (Internet Message Access Protocol) • Library • Connected