Click here to Skip to main content
15,914,222 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

 
QuestionHow to retrieve outlook form template data from the mails? Pin
Daisy Dog4-Aug-05 2:09
Daisy Dog4-Aug-05 2:09 
GeneralUpdated: Search Criteria builder class Pin
GotzBoost6-Jul-05 19:56
GotzBoost6-Jul-05 19:56 
Generalssl Pin
frinomax30-Jun-05 7:40
frinomax30-Jun-05 7:40 
GeneralRe: ssl Pin
mailman23-Jan-08 6:13
mailman23-Jan-08 6:13 
GeneralRe: ssl Pin
Rohit Joshi7-Jun-12 7:55
Rohit Joshi7-Jun-12 7:55 
GeneralSaving attachments Pin
mohdowais16-Jun-05 2:44
mohdowais16-Jun-05 2:44 
GeneralRe: Saving attachments Pin
SnuhEyeless27-Nov-05 16:33
SnuhEyeless27-Nov-05 16:33 
GeneralRe: Saving attachments Pin
superdawgie30-Nov-05 17:03
superdawgie30-Nov-05 17:03 
I have the same problem with receiving large attachments (small attachments come down ok) from exchange.
The streamreader inside readbuffer seems to give up after ?? bytes. Im not sure if its being terminated by some network outage/collision, or its something specific to do with the exchange server IMAP connection.

inside the ReceiveBuffer function is the cBuff character array that the streamreader reads the attachment into. This is made the size of nSize which is the attachment size. So the attachment should fit into it nicely.

What seems to be happening is the streamreader is terminating before its read the full nSize bytes into the cBuff array.
The "& # x 0 ;" are null characters inside the array that make up the rest of the unallocated space between where the streamreader terminated and the end of the cBuff array.

I dont have time to figure out why this is happening so i just made this quick workaround for the ReceiveBuffer function.


protected int ReceiveBuffer (ref string sBuffer, int nSize)
{
int nRead = -1;
char [] cBuff = new Char[nSize];

// nRead will be the number of bytes the streamreader
// actually inserts into the array before terminating

nRead = m_oRdStrm.Read(cBuff, 0, nSize);


// so if the bytes read is smaller than the bytes allocated
// read again with an offset of the sum of nRead's and the
// size of total bytes minus nRead.

// keep looping and adding nRead as you go
// eventually it will get to the end

while(nRead<nSize)
{
nRead += m_oRdStrm.Read(cBuff, nRead, nSize-nRead);
}

string sTmp = new String(cBuff);

sBuffer = sTmp;

// i had to comment out the log function cause
// it was giving me grief with this

// Log (LogTypeEnum.IMAP, sBuffer);

return nRead;
}


After adding the while loop. i was able to read attachments of any size into the cBuff array.


superdawgie

-- modified at 23:04 Wednesday 30th November, 2005
GeneralRe: Saving attachments Pin
alcostas7624-May-06 2:09
alcostas7624-May-06 2:09 
GeneralRe: Saving attachments Pin
pieran3-Aug-06 5:14
pieran3-Aug-06 5:14 
GeneralRe: Saving attachments Pin
mschelstrate4-Feb-10 3:38
mschelstrate4-Feb-10 3:38 
Generali can't write correctly data with NetworkStream Pin
fjlherrera3-May-05 11:42
fjlherrera3-May-05 11:42 
GeneralExposing m_nTotalMessages and others... Pin
wells oliver3-May-05 10:19
wells oliver3-May-05 10:19 
GeneralE-mail file name &amp; Attachment file name Pin
little_duck4-Feb-05 2:49
little_duck4-Feb-05 2:49 
GeneralRe: E-mail file name &amp; Attachment file name Pin
Rohit Joshi7-Feb-05 7:06
Rohit Joshi7-Feb-05 7:06 
GeneralGetMsgFlags Pin
little_duck29-Jan-05 9:24
little_duck29-Jan-05 9:24 
GeneralRe: GetMsgFlags Pin
Rohit Joshi1-Feb-05 3:26
Rohit Joshi1-Feb-05 3:26 
GeneralRe: GetMsgFlags Pin
chietz8-May-05 14:53
chietz8-May-05 14:53 
Questionany known bugs? Pin
jan virin28-Jan-05 18:40
jan virin28-Jan-05 18:40 
AnswerRe: any known bugs? Pin
Rohit Joshi1-Feb-05 3:23
Rohit Joshi1-Feb-05 3:23 
Generalextract E-mail header and store it into database Pin
little_duck21-Jan-05 9:31
little_duck21-Jan-05 9:31 
GeneralRe: extract E-mail header and store it into database Pin
Rohit Joshi21-Jan-05 13:03
Rohit Joshi21-Jan-05 13:03 
GeneralRe: extract E-mail header and store it into database Pin
Anonymous23-Jan-05 7:47
Anonymous23-Jan-05 7:47 
GeneralRe: extract E-mail header and store it into database Pin
little_duck23-Jan-05 8:58
little_duck23-Jan-05 8:58 
GeneralRe: extract E-mail header and store it into database Pin
little_duck23-Jan-05 9:55
little_duck23-Jan-05 9:55 

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.