4.13.1.2. Protocol states

IMAP defines four protocol states. Most commands are valid only in certain states. IMAP has the following states:

  • Non-Authenticated State: This state is at the beginning of the protocol flow before the client authenticates him/herself.

  • Authenticated State: In this state the client is authenticated and MUST select a mailbox to access before commands that affect messages are be permitted.

  • Selected State: In this state, a mailbox is selected for access. The protocol enters this state when a mailbox has been successfully selected.

  • Logout State: In this state the connection is being terminated and the server will close the connection.

IMAP is similar to other protocols in the sense that a connection is authenticated once, at the beginning of the communication. Before authentication is performed only a limited set of commands can be issued, for example AUTHENTICATE and LOGIN.

Each IMAP operation requires a current mailbox which is similar to the current working directory on UNIX systems. Without a selected mailbox, only a limited set of commands can be issued, for example SELECT, CREATE or REMOVE.

Once a mailbox is selected using the SELECT command, further operations become available, like FETCH or STORE.

Example 4.23. IMAP protocol sample
* OK newmail IMAP server ready
A001 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 ACL QUOTA LITERAL+\
        MAILBOX-REFERRALS NAMESPACE UIDPLUS ID\
        NO_ATOMIC_RENAME UNSELECT CHILDREN\
        MULTIAPPEND SORT THREAD=ORDEREDSUBJECT\
        THREAD=REFERENCES IDLE STARTTLS LISTEXT\
        LIST-SUBSCRIBED ANNOTATEMORE
A001 OK Completed
A002 LOGIN user password
A002 OK User logged in
A003 SELECT INBOX
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen)
* OK [PERMANENTFLAGS (\Answered \Flagged \Draft\
        \Deleted \Seen \*)]
* 1094 EXISTS
* 3 RECENT
* OK [UNSEEN 1092]
* OK [UIDVALIDITY 1047554575]
* OK [UIDNEXT 36885]
A003 OK [READ-WRITE] Completed
A004 FETCH 1 RFC822
* 1 FETCH (RFC822 {12}
123456789012
)
A004 OK Completed
A005 LOGOUT
* BYE LOGOUT received
A005 OK Completed

Responses to IMAP requests come in two types: tagged and untagged. When a client issues a request, the server responds with a single tagged response, which may be preceeded by a number of untagged response lines. In the example above, the client issues a tagged A001 CAPABILITY command to ask the server for the supported capabilities. The server replies with the untagged * CAPABILITY IMAP4 ... line, listing the capabilities, and the tagged A001 OK Completed line, indicating that the request was successfully completed.