May 06

I  have try to send email through smtp service using telnet, it occur to me how to access the imap from telnet ? Here is what i got from googling

What is IMAP ?

IMAP stands for Internet Message Access Protocol. It is a method of accessing electronic mail or bulletin board messages that are kept on a (possibly shared) mail server. In other words, it permits a “client” email program to access remote message stores as if they were local. For example, email stored on an IMAP server can be manipulated from a desktop computer at home, a workstation at the office, and a notebook computer while traveling, without the need to transfer messages or files back and forth between these computers.

What is Telnet ?

Telnet is a user command and an underlying TCP/IP protocol for accessing remote computers. Through Telnet, an administrator or another user can access someone else’s computer remotely

Okay let’s start using telnet to connect imap , But before we start to use it there are several thing that you might know that by using telnet it means you use a insecure login , by insecure I just mean that your username and password are sent unencrypted over the internet so potentially could be intercepted on the route between your computer and the mail server. Another alternative, if your email provider supports SSL, is to use OpenSSL (which most if not all Linux computers will have).

And If you make a mistake in a telnet session you cannot use backspace to delete the entry, you may have to press enter to get an error and then re-type the command or quit or ctrl + ]  -> type quit and start again.

Start the telnet

Open your shell and type telnet yourmailserver name /address port 143

# telnet mail.yourserver.com 143

You should get this result:

telnet mail.myserver.com 143
Trying 127.0.0.1…
Connected to mail.yourserver.com (127.0.0.1).
Escape character is ‘^]’.
* OK IMAP4 ready

Loggin to imap account 

Next we need to log in using the login command. Type ‘. login’ followed by your username and password separated by spaces.

. login accountname@yourserver.com *********
. OK User logged in

LIST command

To see a list of all the mailboxes on the server we use the list command. The arguments “” “*” simply get all the mailboxes including sub folders.

. list “” “*”
* LIST (HasNoChildren) “.” “INBOX.Sent”
* LIST (HasNoChildren) “.” “INBOX.Trash”
* LIST (Marked HasChildren) “.” “INBOX”
* LIST (HasNoChildren) “.” “INBOX.Drafts”
. OK LIST completed

We can see from this output how the mailboxes are arranged like a tree with INBOX being the ‘trunk’. My IMAP provider uses a period (.) as a separator between parent and
child folders so INBOX.Drafts is a child of the INBOX. The HasChildren simply tells us that this folder has sub folders whereas the other folders do not.
The way IMAP works means that all folders are created as subfolders of the INBOX even if your email client is configured not to show it that way.
STATUS command.

This command return some basic information on the folder without selecting the folder, it takes arguments depending on what information you would like returned.
Here are 3 example showing total messages, recent messages and unseen messages.

. status inbox (messages)
* STATUS “inbox” (MESSAGES 122)
. OK STATUS Completed.

. status inbox (recent)
* STATUS “inbox” (RECENT 0)
. OK STATUS Completed.

. status inbox (unseen)
* STATUS “inbox” (UNSEEN 67)
. OK STATUS Completed. 

EXAMINE and SELECT commands

These two commands basically do the same thing, they return information on the folder chosen and then allow us to access the mails stored inside the folder. The
main difference is that EXAMINE returns a read-only reference whereas SELECT is read-write.

. examine INBOX.Sent
* FLAGS (Draft Answered Flagged Deleted Seen Recent)
* OK [PERMANENTFLAGS ()] No permanent flags permitted
* 28 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1225959079] Ok
* OK [MYRIGHTS “acdilrsw”] ACL
. OK [READ-ONLY] Ok

. select inbox
* FLAGS (Draft Answered Flagged Deleted Seen Recent)
* OK [PERMANENTFLAGS (* Draft Answered Flagged Deleted Seen)] Limited
* 122 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1225958233] Ok
* OK [MYRIGHTS “acdilrsw”] ACL
. OK [READ-WRITE] Ok

 

Note that the only difference in response is the [READ-ONLY] and [READ-WRITE] text. Basically this command just tells us the possible IMAP flags we can set,
EXIST is how many mails are in the folder, RECENT is how many recent mails there are (the SELECT command will remove the RECENT flag since the folder has now been
viewed, this is not the same as the Seen IMAP flag, also note that the EXAMINE command will not reset the RECENT flag).

The RECENT data is what tells an IMAP email client if you have new mails, by clicking on the folder the client sends the SELECT command and the new mail icon
disappears even though the mails are still unread.

CREATE, DELETE and RENAME folders

It’s very easy to create and delete folders, just make sure you create them as subfolders of the INBOX. For example to create a top level folder called test3 do
do the following.

. create inbox.test1
. OK “inbox.test1” created.
. list “” “*”                                                                 # to check the inbox that we just created
* LIST (HasNoChildren) “.” “INBOX.Sent”
* LIST (HasNoChildren) “.” “INBOX.Trash”
* LIST (Marked HasChildren) “.” “INBOX”
* LIST (HasNoChildren) “.” “INBOX.Drafts”
* LIST (HasNoChildren) “.” “INBOX.test1”  #we created this
. OK LIST completed

Conversely we can delete our new folder using the DELETE command. Note that you cannot delete a folder that had subfolders without first deleting the subfolders, also
deleting a folder containing mails will delete all the mails inside so beware!

. delete inbox.test1
. OK Folder deleted.
. list “” “*”
* LIST (HasNoChildren) “.” “INBOX.Sent”
* LIST (HasNoChildren) “.” “INBOX.Trash”
* LIST (Marked HasChildren) “.” “INBOX”
* LIST (HasNoChildren) “.” “INBOX.Drafts”
. OK LIST completed

Renaming a folder is just as easy, just type RENAME [current name] [new name]. This will not delete mails as they will just exist in the new folder. Here we rename
folder test1 to linux.

. create inbox.test2                          # create thenew inbox that we will rename
. list “” “*”
* LIST (HasNoChildren) “.” “INBOX.Sent”
* LIST (HasNoChildren) “.” “INBOX.Trash”
* LIST (Marked HasChildren) “.” “INBOX”
* LIST (HasNoChildren) “.” “INBOX.test2”
* LIST (HasNoChildren) “.” “INBOX.Drafts”
. OK LIST completed

. rename inbox.test2 inbox.test3
. OK Folder renamed.
. list “” “*”
* LIST (HasNoChildren) “.” “INBOX.Sent”
* LIST (HasNoChildren) “.” “INBOX.Trash”
* LIST (Marked HasChildren) “.” “INBOX”
* LIST (HasNoChildren) “.” “INBOX.Drafts”
* LIST (HasNoChildren) “.” “INBOX.test3”
. OK LIST completed

FETCH command

This command is the main command we use to actually access our emails. It has many possible options depending in what you wish to see, message flags, email headers,
text of the body etc. Here we select the INBOX and fetch the emails in a few different ways.

. select INBOX
* FLAGS (Draft Answered Flagged Deleted Seen Recent)
* OK [PERMANENTFLAGS (* Draft Answered Flagged Deleted Seen)] Limited
* 122 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1225958233] Ok
* OK [MYRIGHTS “acdilrsw”] ACL
. OK [READ-WRITE] Ok

First we shall fetch the message IMAP flags for all the messages in the folder.

. fetch 1:2 flags
* 1 FETCH (FLAGS (Seen))
* 2 FETCH (FLAGS (Seen))
. OK FETCH completed.

Note that with all the commands that act upon messages we can select either 1 message by using the message number as in ‘fetch 1 command’ or we can select a range
of messages in the format ‘fetch first:last command’ or all the messages ‘fetch 1:last command’. Also note that we can use ‘*’ to indicate all messages so fetch 1:* will get
all the messages from the first to the last without us knowing how many messages are in the folder.

First we shall fetch using fast, all and full options (these refer to the headers).

. fetch 1 fast
* 1 FETCH (FLAGS (Seen) INTERNALDATE “06-Nov-2008 15:11:20 +0700” RFC822.SIZE 1377)
. OK FETCH completed.

. fetch 1 all
* 1 FETCH (FLAGS (Seen) INTERNALDATE “06-Nov-2008 15:11:20 +0700” RFC822.SIZE 1377 ENVELOPE (“6 Nov 2008 08:11:20 -0000” “failure notice” ((NIL NIL “MAILER-DAEMON” “yourserver.com”)) ((NIL NIL “MAILER-DAEMON” “yourserver.com”)) ((NIL NIL “MAILER-DAEMON” “yourserver.com”)) ((NIL NIL “adityo” “yourserver.com”)) NIL NIL NIL NIL))
. OK FETCH completed.

. fetch 1 full
* 1 FETCH (FLAGS (Seen) INTERNALDATE “06-Nov-2008 15:11:20 +0700” RFC822.SIZE 1377 ENVELOPE (“6 Nov 2008 08:11:20 -0000” “failure notice” ((NIL NIL “MAILER-DAEMON” “yourserver.com”)) ((NIL NIL “MAILER-DAEMON” “yourserver.com”)) ((NIL NIL “MAILER-DAEMON” “yourserver.com”)) ((NIL NIL “adityo” “yourserver.com”)) NIL NIL NIL NIL) BODY (“text” “plain” NIL NIL NIL “8bit” 1127 31))
. OK FETCH completed.

As you can see this returns differing amounts of data about the IMAP flags, size and ENVELOPE information. It’s maybe more informative to use either ‘fetch message body[header]’
or ‘fetch message rfc822.header’ both of which return the data below.

. fetch 1 rfc822.header
* 1 FETCH (RFC822.HEADER {250}
Return-Path: <>
Delivered-To: adityo@yourserver.com
Received: (qmail 8527 invoked for bounce); 6 Nov 2008 08:11:20 -0000
Date: 6 Nov 2008 08:11:20 -0000
From: MAILER-DAEMON@yourserver.com
To: adityo@yourserver.com
Subject: failure notice

)
. OK FETCH completed.

 

To fetch only some headers we can select the header fields we wish to see.

. fetch 1 (body[header.fields (from to subject date)])
* 1 FETCH (BODY[HEADER.FIELDS (“from” “to” “subject” “date”)] {125}
Date: 6 Nov 2008 08:11:20 -0000
From: MAILER-DAEMON@yourserver.com
To: adityo@yourserver.com
Subject: failure notice

)
. OK FETCH completed.

 

To read the body of the email message we can use either ‘fetch message body[text]’ or ‘fetch message rfc822.text’ as shown here.

. fetch 2 rfc822.text
* 2 FETCH (RFC822.TEXT {8}
tset

)
. OK FETCH completed.

STORE command

This command allows us to add, remove or replace the IMAP flags on the messages. These are flags that denote a message as replied to, deleted, seen etc. and allow
the message information, as well as the message itself, to be synchronized across different computers. Note that the STORE command causes an automatic FETCH
command of the message flags so we can see the change immediately. There are 3 ways to use STORE:

    * STORE message +flags [flag list]  – this adds the [flag list] flags to the chosen messages.

    * STORE message -flags [flag list]  – this removes the [flag list] flags from the chosen messages.

    * STORE message flags [flag list]  – resets the flags to [flag list] on the chosen messages (the same as removing all flags and then adding [flag list].

The list of flags to add include Answered Flagged Draft Deleted Seen and many more. All the IMAP flags used as part of the standard installation have the
backslash in front of them. However some email clients (Thunderbird is one) also allow you to set labels or mark a message as junk, if you add labels do not use
the backslash. First we shall mark all the messages as deleted.

. OK FETCH completed.
. store 1:2 flags Deleted
* 1 FETCH (FLAGS (Deleted))
* 2 FETCH (FLAGS (Deleted))
. OK STORE completed.

Next replace the flags with $label1

. store 1:* flags $label1
* FLAGS ($label1 Draft Answered Flagged Deleted Seen Recent)
* OK [PERMANENTFLAGS ($label1 * Draft Answered Flagged Deleted Seen)] Limited
* 3 FETCH (FLAGS ($label1))
* 4 FETCH (FLAGS ($label1))
* 5 FETCH (FLAGS ($label1))
. OK STORE completed.

Finally we can add the flag NonJunk so that Thunderbird recognises them as not being junk mail.

. store 1:* +flags NonJunk
* FLAGS (NonJunk $label1 Draft Answered Flagged Deleted Seen Recent)
* OK [PERMANENTFLAGS (NonJunk $label1 * Draft Answered Flagged Deleted Seen)] Limited
* 3 FETCH (FLAGS ($label1 NonJunk))
* 4 FETCH (FLAGS ($label1 NonJunk))
* 5 FETCH (FLAGS ($label1 NonJunk))
. OK STORE completed.

Note that the Deleted flag is used by an IMAP server to mark an email ready for deletion, it is not actually deleted until the server receives either the CLOSE
or EXPUNGE command shown below.

CLOSE and EXPUNGE commands

Both these commands have the effect of permanently deleting any messages in the current folder marked for deletion with the Deleted flag. EXPUNGE just deletes the
messages but does nothing else (this command is the equivalent of compacting folders in Thunderbird), while CLOSE deletes the messages and deselects the current
folder (you cannot carry out more action on messages until you select a new folder). Assuming the two messages in our INBOX had the Deleted flag set then the output
looks like the following.

. OK STORE completed.
. expunge
* 1 EXPUNGE
* 1 EXPUNGE
* 120 EXISTS
* 0 RECENT
. OK EXPUNGE completed

COPY command

IMAP has no built in move command, when you move a message you actually copy it to another folder and then delete the original. We can easily copy any number of
messages using the COPY message [destination] format. Here I copy both messages from the INBOX (that I already have selected) to INBOX.test2 folder, after that I
select INBOX.test2 to confirm the messages are there. Note that after copying the RECENT flag is reset.

. copy 1:2 inbox.test3
. OK [COPYUID 1241594365 3:4 1:2] COPY completed.

. select inbox.test3
* FLAGS (NonJunk $label1 Draft Answered Flagged Deleted Seen Recent)
* OK [PERMANENTFLAGS (NonJunk $label1 * Draft Answered Flagged Deleted Seen)] Limited
* 2 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1241594365] Ok
* OK [MYRIGHTS “acdilrsw”] ACL
. OK [READ-WRITE] Ok

IDLE command

IDLE allows us to constantly monitor a folder so that we will be instantly be notified if a new message arrives in the current folder. This is of little use
while in a telnet session but I’ll show it here just so you know how it works. First start IDLE on the folder.

. idle
+ entering idle mode

The server responds with +idling and will stay this way until either a message is received, we stop idling or carry out another command to break the idle. If non
of these things happen then the connection will eventually time out after a pre-set period depending on your IMAP provider (30 minutes in my case). To stop the IDLE
command use DONE (note this is the only command WITHOUT the preceding command tag).

done
. OK IDLE completed

LSUB, SUBSCRIBE and UNSUBSCRIBE commands

These are more commands that only really apply to email clients since they involve subscribing to folders, however they are shown here for completeness. First LSUB
works like LIST with the same arguments but returns a list of the currently subscribed folders.

. lsub “” “*”
* LSUB (HasNoChildren) “.” “INBOX.Drafts”
* LSUB (HasNoChildren) “.” “INBOX.Trash”
* LSUB (HasNoChildren) “.” “INBOX.Sent”
* LSUB (Noselect HasChildren) “.” “INBOX”
. OK LSUB completed

This shows that all folders except INBOX.test3 are currently subscribed, to subscribe a new folder use SUBSCRIBE [foldername].

. subscribe INBOX.test3
. OK Folder subscribed.

To unsubscribe from this folder  use UNSUBSCRIBE [foldername].

. unsubscribe INBOX.test3
. OK Folder unsubscribed.

LOGOUT command

Of course we need to log out of the server, we do this with the LOGOUT command.

. logout
* BYE Courier-IMAP server shutting down
. OK LOGOUT completed
Connection closed by foreign host

That’s the main commands covered however there are a few more just 3 of which I’ll mention here as they could be useful.

CAPABILITY, GETQUOTAROOT AND GETACL commands

These 3 commands return general information on the server environment and your account information. CAPABILITY returns a long list of the mail servers option most of
which are not very exciting, the most important one listed is probably IDLE letting you know that your provider supports the IDLE command. The CHILDREN entry we saw
returned when we did a LIST command (HasChildren or HasNoChildren depending on whether a folder has subfolders).

# telnet mail.yourserver.com 143

. login accountname@yourserver.com *********
. OK User logged in

. capability
* CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA IDLE ACL ACL2=UNION STARTTLS
. OK CAPABILITY completed

GETQUOTAROOT return the amount of space you are using and the amount you have available.

. getquotaroot inbox
* QUOTAROOT “inbox” “ROOT”
* QUOTA “ROOT”
. OK GETQUOTAROOT Ok.

As you can see I’m using 31Mb but have 2Gb capacity, so plenty to spare! Finally GETACL returns the access control list, basically a list of permission you have on
your mail folders.

. getacl inbox
* ACL “inbox” “owner” “acdilrsw”
. OK GETACL completed

These letters each refer to a different permission, the letters after the user are that users rights, the full list is explained here:

   

 l – lookup_flag mailbox is visible to LIST/LSUB commands
r – read_flag SELECT the mailbox, perform CHECK, FETCH, PARTIAL SEARCH, COPY from mailbox
 s – seen_flag keep seen/unseen information across session
w – write_flag STORE flags other than SEEN and DELETED
i – insert_flag perform APPEND, COPY into mailbox
p – post_flag send mail to submission address for mailbox
c – create_flag CREATE new sub-mailboxes in any implementation defined hierarchy
d – delete_flag STORE DELETED flag perform EXPUNGE
a – administer_flag perform SETACL

4 Responses to “How to Access IMAP email accounts using telnet”

  1. riandragon89 Says:

    nich materi matkul PSBD ya sob?

  2. Adityo Says:

    wah ane udah gak kul lagi pak 🙂 udah alumni, PSBD apaan ya ?

  3. deanet Says:

    yah… ketemu adit lagi… 😀 … gmna kabarna dit ?? 🙂

  4. deanet Says:

    *eh mas adit.. maap lupa pakew mas 😀 🙂

Leave a Reply