Test IMAP with telnet
IMAP connection theory
The first thing any client needs to do is to make a connection to the remote imap server on a specific port. These ports can vary, but default port for plaintext imap service is 143
, and for encrypted (SSL/TLS) imap service is default port 993
.
You can use for plaintext imap connection a telnet utility, or for encrypted imap service you can use openssl s_client command.
syntax:
# for plaintext imap service
telnet imap_dns_server_name_or_ip 143
# imap service over SSL/TLS
openssl s_client -connect imap_dns_server_name_or_ip:993
example how connect to imap service with telnet client:
~] telnet mailserver.secar.cz 143
Trying 192.168.0.207...
Connected to mailserver.secar.cz.
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE STARTTLS AUTH=PLAIN AUTH=LOGIN] Dovecot ready.
example how to make connection to imap service with SSL/TLS
~] openssl s_client -connect mailserver.secar.cz:993
...some SSL/TLS handshake...
* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE AUTH=PLAIN AUTH=LOGIN] Dovecot ready.
- mailserver.secar.cz - DNS name of my imap server
- 143 and 993 - port numbers where my imap server listen
IMAP commands format
A imap command, usually, has to be executed in the proper sequence and only if IMAP status has the correct value for that command.
IMAP commands generally look like this:
# Command Input :
<RandomStringID> <command> [<arg1><arg2>…]
# IMAP server answer:
<RandomStringID> OK <ANSWER DETAIL>
Where *
are called untaggged responses, which means that they don’t represent the completion of a command requested by the client. In this case, the untagged response is the capability list, and the tagged response is the OK
status of the command.
This tagging ability means that it’s possible for a server to handle more than one request at the same time from a client on the same connection, and to indicate their completion by sending back the appropriate tag. In practice, many clients don’t support this ability to send concurrent requests, and simply block waiting for data to arrive on the open socket after a command is sent.
Here are some useful imap commands:
LOGIN [username] [password]
LIST [flags] [folder separator] [search term]
STATUS [mailbox] [flags]
SELECT [mailbox]
FETCH [first]:[last] flags
FETCH [mail number] body[header]
FETCH [mail number] body[text]
LOGOUT
IMAP Examples
login to imap server
~] telnet mailserver.secar.cz 143
Trying 192.168.0.207...
Connected to mailserver.secar.cz.
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE STARTTLS AUTH=PLAIN AUTH=LOGIN] Dovecot ready.
a1 login example@sherlog.cz 12345
a1 OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS SPECIAL-USE BINARY MOVE] Logged in
- mailserver.secar.cz - DNS name of my imap server
- 143 - port number where my imap server listen
- example@sherlog.cz - imap login username
- 12345 - password for user example@sherlog.cz
login to imap server and examine inbox
~] telnet mailserver.secar.cz 143
telnet: Trying 192.168.0.207...
telnet: Connected to mailserver.secar.cz.
telnet: Escape character is '^]'.
server: * OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE STARTTLS AUTH=PLAIN AUTH=LOGIN] Dovecot ready.
client: a1 login example@sherlog.cz 12345
server: a1 OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS SPECIAL-USE BINARY MOVE] Logged in
client: a2 LIST "" "*"
server: * LIST (\HasNoChildren) "." Trash
server: * LIST (\HasNoChildren) "." Spam
server: * LIST (\HasNoChildren) "." Sent
server: * LIST (\HasNoChildren) "." Drafts
server: * LIST (\HasNoChildren) "." INBOX
server: a2 OK List completed.
client: a3 EXAMINE INBOX
server: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
server: * OK [PERMANENTFLAGS ()] Read-only mailbox.
server: * 1 EXISTS
server: * 1 RECENT
server: * OK [UNSEEN 1] First unseen.
server: * OK [UIDVALIDITY 1630066715] UIDs valid
server: * OK [UIDNEXT 2] Predicted next UID
server: a3 OK [READ-ONLY] Examine completed (0.000 secs).
client: a4 FETCH 1 BODY[]
server: * 1 FETCH (BODY[] {405}
server: Return-Path: sender@example.com
server: Received: from client.example.com ([192.0.2.1])
server: by mx1.example.com with ESMTP
server: id <20040120203404.CCCC18555.mx1.example.com@client.example.com>
server: for <recipient@example.com>; Tue, 28 Aug 2021 22:34:24 +0200
server: From: sender@example.com
server: Subject: Test message
server: To: example@sherlog.cz
server: Message-Id: <20040120203404.CCCC18555.mx1.example.com@client.example.com>
server:
server: This is a test message.
server: )
server: a4 OK Fetch completed.
client: a5 LOGOUT
server: * BYE Logging out
server: a5 OK Logout completed.
- example@sherlog.cz - imap login username
- 12345 - password for user example@sherlog.cz