NetA2008-Lecture 11: Wednesday February, 2008: 20 Mail Protocols


Table of Contents

Simple Mail Transfer Protocol (SMTP)
Post Office Protocol (PoP)
Internet Message Access Protocol (IMAP)
References

There are three protocols we will consider in a bit of detail:

Simple Mail Transfer Protocol (SMTP)

This is the protocol that actually transfers mail from sender to receiver, between endpoints of the message and/or forwarding agents.

Some sample code (not recently tested) is shown in Networks2001F/Mail/jdh to give an idea of what this kind of code might look like.

The JavaMail system has a full implementation of SMTP and MIME and other related protocols. There are also open source mailing systems written using JavaMail, the main advantage as usual being their portability to any operating system.

My favorite mail transfer system (entirely written in somewhat portable C) is qmail (by Dan Bernstein) and Dan provides a nice and very correct description of SMTP at SMTP:Simple Mail Transfer Protocol . Be sure to read How the client transfers mail which tells just about all you need to know!

Post Office Protocol (PoP)

This protocol is used by a client to retrieve mail from storage at a server. It is very popular, because it is easy to implement at both the client and the server, and requires no administration by the user.

Most experts (people who complicate your life enormously for what they believe to be good reasons) recommend IMAP.

The standard is Post Office Protocol - Version 3 PoP which describes the possible states and the commands available in each state, along with the replies and their reply codes. The states are

  • AUTHORIZATION
  • TRANSACTION
  • UPDATE

The commands and the states in which they can be used are

  • STAT in TRANSACTION
  • LIST in TRANSACTION
  • RETR in TRANSACTION
  • DELE in TRANSACTION
  • NOOP in TRANSACTION
  • RSET in TRANSACTION
  • QUIT in AUTHORIZATION or UPDATE

along with some optional commands.

A typical session is also described in the standard: the following text is a copy of that description:

10. Example POP3 Session

      S: <wait for connection on TCP port 110>
      C: <open connection>
      S:    +OK POP3 server ready <1896.697170952@dbc.mtview.ca.us>
      C:    APOP mrose c4c9334bac560ecc979e58001b3e22fb
      S:    +OK mrose's maildrop has 2 messages (320 octets)
      C:    STAT
      S:    +OK 2 320
      C:    LIST
      S:    +OK 2 messages (320 octets)
      S:    1 120
      S:    2 200
      S:    .
      C:    RETR 1
      S:    +OK 120 octets
      S:    <the POP3 server sends message 1>
      S:    .
      C:    DELE 1
      S:    +OK message 1 deleted
      C:    RETR 2
      S:    +OK 200 octets
      S:    <the POP3 server sends message 2>
      S:    .
      C:    DELE 2
      S:    +OK message 2 deleted
      C:    QUIT
      S:    +OK dewey POP3 server signing off (maildrop empty)
      C:  <close connection>
      S:  <wait for next connection>

Internet Message Access Protocol (IMAP)

This protocol is used by a client which wishes to interact with mail stored on a server.

In the standard RFC2060 we find the major descriptions

  • commands: in section 6
    • 6.1.1. CAPABILITY Command The CAPABILITY command requests a listing of capabilities that the server supports.
         Example:    C: abcd CAPABILITY
                     S: * CAPABILITY IMAP4rev1 AUTH=KERBEROS_V4
                     S: abcd OK CAPABILITY completed
      
    • 6.1.2. NOOP Command The NOOP command always succeeds. It does nothing.
         Example:    C: a002 NOOP
                     S: a002 OK NOOP completed
                        . . .
                     C: a047 NOOP
                     S: * 22 EXPUNGE
                     S: * 23 EXISTS
                     S: * 3 RECENT
                     S: * 14 FETCH (FLAGS (\Seen \Deleted))
                     S: a047 OK NOOP completed
      
    • 6.1.3. LOGOUT Command The LOGOUT command informs the server that the client is done with the connection.
         Example:    C: A023 LOGOUT
                     S: * BYE IMAP4rev1 Server logging out
                     S: A023 OK LOGOUT completed
      
      
    • 6.2.1. AUTHENTICATE Command The AUTHENTICATE command indicates an authentication mechanism, such as described in [IMAP-AUTH], to the server.
         Example:    S: * OK KerberosV4 IMAP4rev1 Server
                     C: A001 AUTHENTICATE KERBEROS_V4
                     S: + AmFYig==
                     C: BAcAQU5EUkVXLkNNVS5FRFUAOCAsho84kLN3/IJmrMG+25a4DT
                        +nZImJjnTNHJUtxAA+o0KPKfHEcAFs9a3CL5Oebe/ydHJUwYFd
                        WwuQ1MWiy6IesKvjL5rL9WjXUb9MwT9bpObYLGOKi1Qh
                     S: + or//EoAADZI=
                     C: DiAF5A4gA+oOIALuBkAAmw==
                     S: A001 OK Kerberos V4 authentication successful
      
    • 6.2.2. LOGIN Command The LOGIN command identifies the client to the server and carries the plaintext password authenticating this user.
         Example:    C: a001 LOGIN SMITH SESAME
                     S: a001 OK LOGIN completed
      
    • 6.3.1. SELECT Command The SELECT command selects a mailbox so that messages in the mailbox can be accessed.
         Example:    C: A142 SELECT INBOX
                     S: * 172 EXISTS
                     S: * 1 RECENT
                     S: * OK [UNSEEN 12] Message 12 is first unseen
                     S: * OK [UIDVALIDITY 3857529045] UIDs valid
                     S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
                     S: * OK [PERMANENTFLAGS (\Deleted \Seen \*)] Limited
                     S: A142 OK [READ-WRITE] SELECT completed
      
    • 6.3.2. EXAMINE Command The EXAMINE command is identical to SELECT and returns the same output; however, the selected mailbox is identified as read-only.
         Example:    C: A932 EXAMINE blurdybloop
                     S: * 17 EXISTS
                     S: * 2 RECENT
                     S: * OK [UNSEEN 8] Message 8 is first unseen
                     S: * OK [UIDVALIDITY 3857529045] UIDs valid
                     S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
                     S: * OK [PERMANENTFLAGS ()] No permanent flags permitted
                     S: A932 OK [READ-ONLY] EXAMINE completed
      
    • 6.3.3. CREATE Command The CREATE command creates a mailbox with the given name. An OK response is returned only if a new mailbox with that name has been created.
         Example:    C: A003 CREATE owatagusiam/
                     S: A003 OK CREATE completed
                     C: A004 CREATE owatagusiam/blurdybloop
                     S: A004 OK CREATE completed
      
    • 6.3.4. DELETE Command The DELETE command permanently removes the mailbox with the given name.
         Examples:   C: A682 LIST "" *
                     S: * LIST () "/" blurdybloop
                     S: * LIST (\Noselect) "/" foo
                     S: * LIST () "/" foo/bar
                     S: A682 OK LIST completed
                     C: A683 DELETE blurdybloop
                     S: A683 OK DELETE completed
                     C: A684 DELETE foo
                     S: A684 NO Name "foo" has inferior hierarchical names
                     C: A685 DELETE foo/bar
                     S: A685 OK DELETE Completed
                     C: A686 LIST "" *
                     S: * LIST (\Noselect) "/" foo
                     S: A686 OK LIST completed
                     C: A687 DELETE foo
                     S: A687 OK DELETE Completed
      
                     C: A82 LIST "" *
                     S: * LIST () "." blurdybloop
                     S: * LIST () "." foo
                     S: * LIST () "." foo.bar
                     S: A82 OK LIST completed
                     C: A83 DELETE blurdybloop
                     S: A83 OK DELETE completed
                     C: A84 DELETE foo
                     S: A84 OK DELETE Completed
                     C: A85 LIST "" *
                     S: * LIST () "." foo.bar
                     S: A85 OK LIST completed
                     C: A86 LIST "" %
                     S: * LIST (\Noselect) "." foo
                     S: A86 OK LIST completed
      
    • 6.3.5. RENAME Command The RENAME command changes the name of a mailbox.
         Examples:   C: A682 LIST "" *
                     S: * LIST () "/" blurdybloop
                     S: * LIST (\Noselect) "/" foo
                     S: * LIST () "/" foo/bar
                     S: A682 OK LIST completed
                     C: A683 RENAME blurdybloop sarasoop
                     S: A683 OK RENAME completed
                     C: A684 RENAME foo zowie
                     S: A684 OK RENAME Completed
                     C: A685 LIST "" *
                     S: * LIST () "/" sarasoop
                     S: * LIST (\Noselect) "/" zowie
                     S: * LIST () "/" zowie/bar
                     S: A685 OK LIST completed
      
                     C: Z432 LIST "" *
                     S: * LIST () "." INBOX
                     S: * LIST () "." INBOX.bar
                     S: Z432 OK LIST completed
                     C: Z433 RENAME INBOX old-mail
                     S: Z433 OK RENAME completed
                     C: Z434 LIST "" *
                     S: * LIST () "." INBOX
                     S: * LIST () "." INBOX.bar
                     S: * LIST () "." old-mail
                     S: Z434 OK LIST completed
      
    • 6.3.6. SUBSCRIBE Command The SUBSCRIBE command adds the specified mailbox name to the server's set of "active" or "subscribed" mailboxes as returned by the LSUB command.
         Example:    C: A002 SUBSCRIBE #news.comp.mail.mime
                     S: A002 OK SUBSCRIBE completed
      
    • 6.3.7. UNSUBSCRIBE Command The UNSUBSCRIBE command removes the specified mailbox name from the server's set of "active" or "subscribed" mailboxes as returned by the LSUB command.
         Example:    C: A002 UNSUBSCRIBE #news.comp.mail.mime
                     S: A002 OK UNSUBSCRIBE completed
      
    • 6.3..8. LIST Command The LIST command returns a subset of names from the complete set of all names available to the client.
         Example:    C: A101 LIST "" ""
                     S: * LIST (\Noselect) "/" ""
                     S: A101 OK LIST Completed
                     C: A102 LIST #news.comp.mail.misc ""
                     S: * LIST (\Noselect) "." #news.
                     S: A102 OK LIST Completed
                     C: A103 LIST /usr/staff/jones ""
                     S: * LIST (\Noselect) "/" /
                     S: A103 OK LIST Completed
                     C: A202 LIST ~/Mail/ %
                     S: * LIST (\Noselect) "/" ~/Mail/foo
                     S: * LIST () "/" ~/Mail/meetings
                     S: A202 OK LIST completed
      
    • 6.3.9. LSUB Command The LSUB command returns a subset of names from the set of names that the user has declared as being "active" or "subscribed".
         Example:    C: A002 LSUB "#news." "comp.mail.*"
                     S: * LSUB () "." #news.comp.mail.mime
                     S: * LSUB () "." #news.comp.mail.misc
                     S: A002 OK LSUB completed
      
    • 6.3.10. STATUS Command The STATUS command requests the status of the indicated mailbox.
            Example:    C: A042 STATUS blurdybloop (UIDNEXT MESSAGES)
                        S: * STATUS blurdybloop (MESSAGES 231 UIDNEXT 44292)
                        S: A042 OK STATUS completed
      
    • 6.3.11. APPEND Command The APPEND command appends the literal argument as a new message to the end of the specified destination mailbox.
         Example:    C: A003 APPEND saved-messages (\Seen) {310}
                     C: Date: Mon, 7 Feb 1994 21:52:25 -0800 (PST)
                     C: From: Fred Foobar <foobar@Blurdybloop.COM>
                     C: Subject: afternoon meeting
                     C: To: mooch@owatagu.siam.edu
                     C: Message-Id: <B27397-0100000@Blurdybloop.COM>
                     C: MIME-Version: 1.0
                     C: Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
                     C:
                     C: Hello Joe, do you think we can meet at 3:30 tomorrow?
                     C:
                     S: A003 OK APPEND completed
      
      
    • 6.4.1. CHECK Command The CHECK command requests a checkpoint of the currently selected mailbox.
         Example:    C: FXXZ CHECK
                     S: FXXZ OK CHECK Completed
      
    • 6.4.2. CLOSE Command The CLOSE command permanently removes from the currently selected mailbox all messages that have the \Deleted flag set, and returns to authenticated state from selected state.
         Example:    C: A341 CLOSE
                     S: A341 OK CLOSE completed
      
    • 6.4.3. EXPUNGE Command The EXPUNGE command permanently removes from the currently selected mailbox all messages that have the \Deleted flag set.
         Example:    C: A202 EXPUNGE
                     S: * 3 EXPUNGE
                     S: * 3 EXPUNGE
                     S: * 5 EXPUNGE
                     S: * 8 EXPUNGE
                     S: A202 OK EXPUNGE completed
      
    • 6.4.4. SEARCH Command The SEARCH command searches the mailbox for messages that match the given searching criteria.
         Example:    C: A282 SEARCH FLAGGED SINCE 1-Feb-1994 NOT FROM "Smith"
                     S: * SEARCH 2 84 882
                     S: A282 OK SEARCH completed
      
    • 6.4.5. FETCH Command The FETCH command retrieves data associated with a message in the mailbox.
         Example:    C: A654 FETCH 2:4 (FLAGS BODY[HEADER.FIELDS (DATE FROM)])
                     S: * 2 FETCH ....
                     S: * 3 FETCH ....
                     S: * 4 FETCH ....
                     S: A654 OK FETCH completed
      
    • 6.4.6. STORE Command The STORE command alters data associated with a message in the mailbox.
         Example:    C: A003 STORE 2:4 +FLAGS (\Deleted)
                     S: * 2 FETCH FLAGS (\Deleted \Seen)
                     S: * 3 FETCH FLAGS (\Deleted)
                     S: * 4 FETCH FLAGS (\Deleted \Flagged \Seen)
                     S: A003 OK STORE completed
      
    • 6.4.7. COPY Command The COPY command copies the specified message(s) to the end of the specified destination mailbox.
         Example:    C: A003 COPY 2:4 MEETING
                     S: A003 OK COPY completed
      
    • 6.4.8. UID Command The UID command has two forms. In the first form, it takes as its arguments a COPY, FETCH, or STORE command with arguments appropriate for the associated command. In the second form, the UID command takes a SEARCH command with SEARCH command arguments.
         Example:    C: A999 UID FETCH 4827313:4828442 FLAGS
                     S: * 23 FETCH (FLAGS (\Seen) UID 4827313)
                     S: * 24 FETCH (FLAGS (\Seen) UID 4827943)
                     S: * 25 FETCH (FLAGS (\Seen) UID 4828442)
                     S: A999 UID FETCH completed
      
  • responses: in section 7
  • syntax: in section 8

some IMAP references. 

Capture of an IMAP session.  Sample IMAP Session

References