Click here to Skip to main content
15,885,216 members
Articles / Web Development / HTML

IMAP Client library using C#

Rate me:
Please Sign up or sign in to vote.
4.65/5 (88 votes)
20 Sep 2012MPL2 min read 1.4M   31.9K   184   406
IMAPLibrary supports the basic IMAP protocol functions to fetch messages from the mailbox.

GitHub Link: https://github.com/rohitjoshi/ImapLibrary

 

Introduction

The Internet Message Access Protocol (IMAP) allows a client to access and manipulate electronic mail messages on a server. It includes operations for creating, deleting, and renaming mailboxes; checking for new messages; permanently removing messages; setting and clearing flags; [RFC-822] and [MIME-IMB] parsing; searching; and selective fetching of message attributes, texts, and portions thereof. For more information: here.

I have written an IMAP client library which allows basic functionalities like login, select/examine folder, search messages, fetch message (Header, Body), get storage quota, and logout.

This is my first application developed in C#, so don't expect too much in terms of efficiency. It demonstrates the use of sockets, XML writer, and user defined exception handling. Please feel free to modify and use this code.

The attached zip file contains three directories.

IMAP Library: It contains three source files.

  • ImapBase.cs: contains the IMAP commands related to string, and socket related functionality.
  • ImapException.cs: defines the user defined IMAP related error messages.
  • Imap.cs: IMAP client library functions. It has the following public functions:
    • Login: Login to IMAP server. It requires IMAP hostname, port, username, and password.
      <COMMAND_PREFIX> LOGIN <USERID> <PASSWORD>\r\n
    • Logout: Logout and close the socket.
      <COMMAND_PREFIX> LOGOUT\r\n
    • SelectFolder: It selects the folder. It requires folder name as parameter.
      <COMMAND_PREFIX> SELECT <FOLDER>\r\n
    • ExamineFolder: It is similar to SelectFolder, but it does examine.
      <COMMAND_PREFIX> EXAMINE <FOLDER>\r\n
    • GetQuota: Get the quota of the mailbox.
      <COMMAND_PREFIX> GETQUOTAROOT <FOLDER>\r\n
    • SearchMessage: You can search the messages. It will return the UID of messages. E.g., From rjoshi.
      <COMMAND_PREFIX> SEARCH <SEARCH STRING>\r\n
    • FetchMessage: It retrieves the complete message with attachments and writes into an XML file. The XML file will be generated in your current directory with file name as <MessageUID>.xml. You need to pass the XmlTextWriter object, message UID, and flag to fetch body.
      <COMMAND_PREFIX> UID FETCH  <MSG UID> BODY[HEADER]
    • FetchPartBody: Fetch the body for a specific part. It requires message UID, part number as parameter.
    • FetchPartHeader: Fetch the header of message.

Documentation: HTML Documentation for IMAP Library generated using Visual Studio .NET.

IMAP Library test program: The IMAP test program allows users to test the following functionalities.

  • Login
  • Select/Examine folder
  • Search
  • Fetch Message
  • Get Quota
  • Logout
  • Delete Message
  • Mark Message UnRead 
  • Move Message

   Image 1
 

Update: Added support for

  1. SSL Connection and verified with gmail
  2. Copy Message
  3. Move Message
  4. Delete Message 
  5. Mark Message Unread  

Please don't forget to Vote if you like this library and PR welcome at github repository!!

 

 

 

 

License

This article, along with any associated source code and files, is licensed under The Mozilla Public License 1.1 (MPL 1.1)


Written By
Software Developer
United States United States
Rohit Joshi is a software engineer working for a telecom company in USA. He has development expirience using C, C++ ,C#, VoiceXML, ASR, IMAP, LDAP, HTTP, SIP, H323 on unix/linux and platforms.

Comments and Discussions

 
GeneralDid SSL ever get added Pin
Scott W Bowyer24-Feb-11 12:27
Scott W Bowyer24-Feb-11 12:27 
GeneralRe: Did SSL ever get added Pin
Dhaidh30-May-12 5:52
Dhaidh30-May-12 5:52 
GeneralRe: Did SSL ever get added Pin
Rohit Joshi6-Jun-12 10:36
Rohit Joshi6-Jun-12 10:36 
GeneralRe: Did SSL ever get added Pin
Rohit Joshi6-Jun-12 10:37
Rohit Joshi6-Jun-12 10:37 
GeneralNewbie Q, i dont understand something Pin
Gothrek21-Feb-11 1:12
Gothrek21-Feb-11 1:12 
GeneralRe: Newbie Q, i dont understand something Pin
Rohit Joshi4-Apr-12 4:02
Rohit Joshi4-Apr-12 4:02 
GeneralRe: Newbie Q, i dont understand something Pin
Rohit Joshi6-Jun-12 10:10
Rohit Joshi6-Jun-12 10:10 
QuestionGetting Wrong Response...help Pin
jymitra28-Dec-10 23:32
jymitra28-Dec-10 23:32 
hi every body ,
whenever i am trying to fetch attachment parts sometimes i got wrong response according to given command to server .

I am doing following process.(with command given and getting responses)

first attachment
-------------------------------
IMAP0011 UID FETCH 660554996 BODY[2]
* 39 FETCH (UID 660554996 BODY[2] {44844}
)
IMAP0011 OK UID FETCH completed
----------------------------------
second attachment
------------------------
IMAP0012 UID FETCH 660554996 BODY[3]
* 39 FETCH (UID 660554996 BODY[3] {1270188}
)
IMAP0012 OK UID FETCH completed
--------------------------------------
Third attachment
--------------------------------------
IMAP0013 UID FETCH 660554996 BODY[4]

after that i have got many times this responses
* 39 FETCH (UID 660554996 BODY[3] * OK Reset Timeout or
simple "* OK Reset Timeout"

but some how i have tried to get response by giving same command again n again and then i got
{1270188} which is previous attachment size(Which is wrong) and then every thing goes wrong with Recieve() method --server give continuous response in byte in Recieve () which should not get.


---------------------
please if anybody have an idea about it and any clue will help me a lot ...

AnswerRe: Getting Wrong Response...help Pin
Rohit Joshi21-Jan-11 9:48
Rohit Joshi21-Jan-11 9:48 
GeneralRe: Getting Wrong Response...help Pin
jymitra21-Jan-11 20:14
jymitra21-Jan-11 20:14 
QuestionMARKING LABELS [modified] Pin
Vaishalimehre13-Oct-10 12:40
Vaishalimehre13-Oct-10 12:40 
QuestionHelp about store flag "UNSEEN" Pin
jymitra11-Oct-10 21:16
jymitra11-Oct-10 21:16 
AnswerRe: Help about store flag "UNSEEN" Pin
Rohit Joshi12-Oct-10 15:34
Rohit Joshi12-Oct-10 15:34 
GeneralRe: Help about store flag "UNSEEN" Pin
jymitra12-Oct-10 20:21
jymitra12-Oct-10 20:21 
GeneralReceiving LONG Messags-Bodies Pin
CFloor5-Oct-10 22:50
CFloor5-Oct-10 22:50 
GeneralRe: Receiving LONG Messags-Bodies Pin
Rohit Joshi12-Oct-10 15:32
Rohit Joshi12-Oct-10 15:32 
GeneralSSL Support Pin
dorphalsig19-Aug-10 8:19
dorphalsig19-Aug-10 8:19 
GeneralRe: SSL Support Pin
Rohit Joshi12-Oct-10 15:31
Rohit Joshi12-Oct-10 15:31 
GeneralRe: SSL Support Pin
mohamed antar20-Nov-10 10:24
mohamed antar20-Nov-10 10:24 
GeneralRe: SSL Support Pin
Rohit Joshi6-Jun-12 10:37
Rohit Joshi6-Jun-12 10:37 
GeneralRe: SSL Support Pin
Rohit Joshi6-Jun-12 10:38
Rohit Joshi6-Jun-12 10:38 
GeneralThanks a lot Pin
Alexander Wieser7-Aug-10 1:40
Alexander Wieser7-Aug-10 1:40 
Questionsomebody wake up Please help about my problems Pin
jymitra30-Jul-10 1:06
jymitra30-Jul-10 1:06 
AnswerRe: somebody wake up Please help about my problems Pin
Rohit Joshi12-Oct-10 15:30
Rohit Joshi12-Oct-10 15:30 
GeneralHelp about my problems :( [modified] Pin
jymitra12-Oct-10 18:57
jymitra12-Oct-10 18:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.