IMAP telnet example (test IMAP with telnet)

Here some notes about IMAP testing using telnet with reference to  email client behaviours (login, listing emails and folders, read email…)

 How to test with curl command :
$curl imap:// (goto curl notes)
extra notes (single command cut&paste) here

#I About identification of an email message

There are two types of ID for an email :

  • UID : Unique Identifier is an unique number referencing an email
  • Message Sequence Number: the relative position from the first message in the mailbox

About UID  :

 the unique identifier of a message MUST NOT change during 
the session, and SHOULD NOT change between sessions. Any change of unique identifiers between 
sessions MUST be detectable using the UIDVALIDITY mechanism discussed below...
... and ...
 Unique identifiers MUST be strictly ascending in the mailbox at all times.
... and more ...
 The combination of mailbox name, UIDVALIDITY, and UID must refer to a single immutable message 
on that server forever.

#II Folders list

Email list folder
Email list folder (client)

Email client can get list of all folders using command : LIST “” “*”
Btw in some cases (depends by use case, number of folders and …) it is better ask just for top level of folders :

a LIST "" %
* LIST (HasChildren) "/" "level1"
...(only root folders)

instead of whole list :

a LIST "" *
* LIST (HasChildren) "/" "leve1"
* LIST (HasChildren) "/" "leve1/leve2"
* LIST (HasNoChildren) "/" "leve1/leve2/leve3"
 ...( all folders hierarchy )

It is better doesn’t expand automatically the whole tree.

#III List of emails in a folder

emails list
emails list in a folder

Build the list of email messages in a folder in 4 steps

1. LOGIN - connect to IMAP Server (click to expand)

telnet <server> 143
login <user> <password>
Example (or for CRAM-MD5 imap login click here) :

telnet <SERVER> 143
Trying <SERVER-IP>...
Connected to <SERVER-IP>
Escape character is '^]'.
* OK IMAP4 server ready
a login password
a OK login successful
2. SELECT - the target folder ..i.e. sent (click to expand)
a SELECT "sent"
* 254 EXISTS
* FLAGS (Answered Flagged Deleted Seen Draft $MDNSent)
* OK [PERMANENTFLAGS (Answered Flagged Deleted Seen Draft $MDNSent)] flags can be changed
* OK [UIDVALIDITY 1360825208] mailbox UID validity
* OK [UIDNEXT 483] predicted next UID
3. SEARCH - obtain list of messages with proper criteria (click to expand)

Example :

 a UID SEARCH BEFORE "01-Jan-2013"
 * SEARCH 42 43 44 20412
 a OK SEARCH complete

Others criteria are :
FLAG (search messages with FLAG seen):
Date :
a UID SEARCH BEFORE “01-Jan-2013”
a UID SEARCH AFTER “01-Jan-2013”
From :
ALL messages :

All above search commands return list of UIDs.

4. FETCH - get emails information useful for list (click to expand)

For each  UID in step 3 … :
a UID SEARCH BEFORE "01-Jan-2013"
* SEARCH 42 43 44 20412
a OK SEARCH complete

(in our example UID are 42, 43, 44 and 20412) we need to execute IMAP FETCH in order to get email details
Example if you need to compose a  list of messages with columns [from] [to] [object] [date] :

a UID FETCH 42 (BODY[HEADER.FIELDS (from to subject date)])
* 30 FETCH (BODY[HEADER.FIELDS (from to subject date)] {162}
Date: Wed, 19 Jan 2011 12:20:16 +0100 (CET)
From: "test2" <>
Subject: Caratteri cinesi

UID 42)
a OK FETCH complete
FROM TO Subject Date
test2 ( Caratteri cinesi Wed, 19 Jan 2011 Test email Tue, 18 Jan 2011

#IV Read/Open email

Message Read
Message Read

In order to read a message user usually selects an email starting by a list of messages (for example using an email client). All messages (in a folder) are identified by an UID (that we have retrieved in step 3 here ). In order to read a message  can be used the FETCH command.
Example :
a login password
a OK login successful
a SELECT “sent”
* 254 EXISTS
… and remaining steps reported above here

* 20310 FETCH (BODYSTRUCTURE ((("TEXT" "PLAIN" ("charset" "UTF-8") NIL NIL "7bit" 4 1)("TEXT" "HTML" ("charset" "UTF-8") NIL NIL "7bit" 4 1) "ALTERNATIVE" ("boundary" "----=_Part_18_28612235.1384442157276"))("IMAGE" "PNG" ("name" "Screen Shot 2013-11-14 at 4.15.41 PM.png") NIL NIL "base64" 13858 NIL ("attachment" ("filename" "Screen Shot 2013-11-14 at 4.15.41 PM.png" "size" "10127"))) "MIXED" ("boundary" "----=_Part_17_22578400.1384442157276")) UID 22049)
a OK FETCH complete

* 20310 FETCH (BODY[1.MIME] {90}
Content-Type: multipart/alternative;

UID 22049)
a OK FETCH complete

a UID FETCH 22049 (BODY[1.1])
* 20310 FETCH (BODY[1.1] {370}
Body MailTest : hello!!!!! {margin:0;padding:0;} #footer { height:13px; font-size:11px; font-family:Arial, FreeSans, sans-serif; color:#ADADAD; margin:0; padding:7px 12px; text-align:right; border-top:1px solid #dcdcdc; }          #footer a { text-decoration:none; color:#ADADAD; } #footer a:hover { color:#848484; } TEST 12   UID 22049)
a OK FETCH complete
.... continue

As said email to read is selected probably from a list. In this case the client has already read some headers. Btw it is better don’t read all whole email (i.e. big attachment  could be present in email). The proper way is fetch header and message body information that you need to show immediately to user (above example). If user click on download attachment then the client can fetch the attachment.
Fetching the attachment it is better fetch pieces (chunking):
a FETCH 1 BODY.PEEK[2]<0.102400>
a FETCH 1 BODY.PEEK[2]<102401.204800>

#V Write / Delete / Update of a message

there are, in IMAP, several write events :

  • store message
  • save draft
  • delete

There isn’t any update command.

Store message

In order to Save email IMAP uses  APPEND (example save sent email in sent folder) :

a APPEND sent (\Seen) {332}
+ Ready for literal data
 Date: Mon, 12 Feb 2013 21:52:25 -0800 (PST)
 From: test me <testme@test.priv>
 Subject:  test append
 To: testyou@test.priv
 Message-Id: <B27397-0100000@abc.priv>
 MIME-Version: 1.0

This is a test body for the message!
a OK

Where :

  • {332} -> is byte dimension on append part
  • sent    ->  is folder
  • Seen   -> is flag

Save draft

Draft message can be saved with IMAP APPEND and flag DRAFT. Draft message has the peculiarity to be an incomplete email message… so  it needs to be updated repeatedly by the user (modify & save & modify & …).  There isn’t any way in order edit/update email message on IMAP server side. The only way in order to update a message is remove it (DELETE) then create it again (APPEND). In this case UID will change for the message.


Delete email is done setting “flag Deleted” then “expunge” (DELETE) flagged messages.
1 – Set deleted flag (message with UID 42) :

a OK STORE complete

2- expunge
Example :
(delete all messages with flag deleted)

  * 42 EXPUNGE
  * 42 EXPUNGE
  * 42 EXPUNGE
  * 42 EXPUNGE
 a OK EXPUNGE complete

or.. example

a OK EXPUNGE complete
Powered by WP Review