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

 
GeneralRe: Bug Pin
Rohit Joshi16-Jan-06 7:25
Rohit Joshi16-Jan-06 7:25 
Generalanother Imap library Pin
eskan11-Nov-05 5:58
eskan11-Nov-05 5:58 
GeneralChange to TestImap.cs Pin
Keith Farmer10-Nov-05 10:48
Keith Farmer10-Nov-05 10:48 
GeneralRetrieve number of unread messages Pin
Psanchez1172-Nov-05 7:36
Psanchez1172-Nov-05 7:36 
GeneralRe: Retrieve number of unread messages Pin
GotzBoost5-Nov-05 19:06
GotzBoost5-Nov-05 19:06 
GeneralRe: Retrieve number of unread messages Pin
Rohit Joshi16-Jun-06 17:08
Rohit Joshi16-Jun-06 17:08 
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 
Here is a class that will build a search string for you if you are trying to use the SEARCH command.
(Note: this is for both the SEARCH command and the UID SEARCH command)

#### Sample use of function
rfc3501SearchCmd Or1;
rfc3501SearchCmd Or2;
rfc3501SearchCmd Srch;
Or1 = new rfc3501SearchCmd().AND(new Criteria().FROM("john")).AND(new Criteria().BODY("testing")).AND(new Criteria().SENTON(DateTime.Now));
Or2 = new rfc3501SearchCmd().AND(new Criteria().FROM("nguyen")).AND(new Criteria().LARGER(100));
Srch = new rfc3501SearchCmd().OR(Or1, Or2);
// This creates the SEARCH string of
// OR (FROM "john" BODY "testing" SENTON "7-Jul-2005") (FROM "nguyen" LARGER 100)
try
{
// o_Imap = Your declared IMAP class, replace with yours.
o_Imap.Search(Srch);
}
catch (ImapException ex)
{
Console.WriteLine("Imap Error: " + ex.Message + " :" + ex.InnerException);
return;
}

#### New Search function for Imap.cs
/// <summary>
/// IMAP SEARCH command
/// </summary>
public virtual void Search(rfc3501SearchCmd CriteriaCmd)
{
Console.WriteLine(CriteriaCmd.ToString());


ImapResponseEnum eImapResponse = ImapResponseEnum.IMAP_SUCCESS_RESPONSE;
ArrayList sResultArray = new ArrayList();
string searchCommand;
searchCommand = IMAP_SEARCH_COMMAND + " " + CriteriaCmd.ToString();
searchCommand += IMAP_COMMAND_EOL;
try
{
eImapResponse = SendAndReceive(searchCommand, ref sResultArray);
if (eImapResponse == ImapResponseEnum.IMAP_SUCCESS_RESPONSE)
{
// Do something with your returned data
}
else
{
string tmpErr = sResultArray[0].ToString().Substring(sResultArray[0].ToString().IndexOf("SEARCH") + 6).Trim();
throw new ImapException(tmpErr);
}
}
catch (Exception e)
{
throw e;
}

}


#### the two new classes (rfc3501SearchCmd.cs)
/// <summary>
/// Builds Complete SEARCH strings for a IMAP4rev1 SEARCH command
/// </summary>
public class rfc3501SearchCmd
{
#region Declerations
protected string g_SearchCrit = string.Empty;
#endregion

public override string ToString(){ return g_SearchCrit.Trim(); }

public rfc3501SearchCmd(){}
#region AND
public rfc3501SearchCmd AND(Criteria Crit)
{
g_SearchCrit += " " + Crit.ToString();
return this;
}
#endregion
#region OR
/// <summary>
/// Messages that match either search key.
/// </summary>
/// <param name="Crit1"></param>
/// <param name="Crit2"></param>
/// <returns></returns>
public rfc3501SearchCmd OR(rfc3501SearchCmd Crit1, rfc3501SearchCmd Crit2)
{
g_SearchCrit += " OR (" + Crit1.ToString() + ") (" + Crit2.ToString() + ")";
return this;
}
#endregion
#region NOT
/// <summary>
/// Messages that do not match the specified search key.
/// </summary>
/// <param name="Crit"></param>
/// <returns></returns>
public rfc3501SearchCmd NOT(rfc3501SearchCmd Crit)
{
g_SearchCrit += " NOT (" + Crit.ToString() + ")";
return this;
}
#endregion
}

/// <summary>
/// Returns built commands to build a entire IMAP4rev1 SEARCH command
/// </summary>
public class Criteria
{
#region Declerations
protected string g_Crit = string.Empty;
#endregion

public override string ToString(){ return g_Crit; }

#region Search Commands
/// <summary>
/// All messages in the mailbox; the default initial key for ANDing.
/// </summary>
/// <returns>It's self</returns>
public Criteria ALL() { g_Crit = "ALL"; return this; }
/// <summary>
/// Messages with the \Answered flag set.
/// </summary>
/// <returns>It's self</returns>
public Criteria ANSWERED() { g_Crit = "ANSWERED"; return this; }
/// <summary>
/// Messages that contain the specified string in the envelope structure's BCC field.
/// </summary>
/// <param name="Text"></param>
/// <returns>It's self</returns>
public Criteria BCC(string Text) { g_Crit = "BCC \"" + Text + "\""; return this; }
/// <summary>
/// Messages that contain the specified string in the body of the
/// message.
/// </summary>
/// <param name="Text"></param>
/// <returns>It's self</returns>
public Criteria BODY(string Text) { g_Crit = "BODY \"" + Text + "\""; return this; }
/// <summary>
/// Messages that contain the specified string in the envelope
/// structure's CC field.
/// </summary>
/// <param name="Text"></param>
/// <returns>It's self</returns>
public Criteria CC(string Text) { g_Crit = "CC \"" + Text + "\""; return this; }
/// <summary>
/// Messages with the \Deleted flag set.
/// </summary>
/// <returns>It's self</returns>
public Criteria DELETED() { g_Crit = "DELETED"; return this; }
/// <summary>
/// Messages with the \Draft flag set.
/// </summary>
/// <returns>It's self</returns>
public Criteria DRAFT() { g_Crit = "DRAFT"; return this; }
/// <summary>
/// Messages with the \Flagged flag set.
/// </summary>
/// <returns>It's self</returns>
public Criteria FLAGGED() { g_Crit = "FLAGGED"; return this; }
/// <summary>
/// Messages that contain the specified string in the envelope
/// structure's FROM field.
/// </summary>
/// <param name="Text"></param>
/// <returns>It's self</returns>
public Criteria FROM(string Text) { g_Crit = "FROM \"" + Text + "\""; return this; }
/// <summary>
/// Messages with an [RFC-2822] size larger than the specified
/// number of octets.
/// </summary>
/// <param name="Size">Size in bytes</param>
/// <returns>It's self</returns>
public Criteria LARGER(int Size) { g_Crit = "LARGER " + Size; return this; }
/// <summary>
/// Messages that have the \Recent flag set but not the \Seen flag.
/// This is functionally equivalent to "(RECENT UNSEEN)".
/// </summary>
/// <returns>It's self</returns>
public Criteria NEW() { g_Crit = "NEW"; return this; }
/// <summary>
/// Messages that do not have the \Recent flag set. This is
/// functionally equivalent to "NOT RECENT" (as opposed to "NOT
/// NEW").
/// </summary>
/// <returns>It's self</returns>
public Criteria OLD() { g_Crit = "OLD"; return this; }
/// <summary>
/// Messages that have the \Recent flag set.
/// </summary>
/// <returns>It's self</returns>
public Criteria RECENT() { g_Crit = "RECENT"; return this; }
/// <summary>
/// Messages that have the \Seen flag set.
/// </summary>
/// <returns>It's self</returns>
public Criteria SEEN() { g_Crit = "SEEN"; return this; }
/// <summary>
/// Messages that contain the specified string in the header or
/// body of the message.
/// </summary>
/// <param name="Text"></param>
/// <returns>It's self</returns>
public Criteria TEXT(string Text) { g_Crit = "TEXT \"" + Text + "\""; return this; }
/// <summary>
/// Messages that contain the specified string in the envelope
/// structure's TO field.
/// </summary>
/// <param name="Text"></param>
/// <returns>It's self</returns>
public Criteria TO(string Text) { g_Crit = "TO \"" + Text + "\""; return this; }
/// <summary>
/// Messages with unique identifiers corresponding to the specified
/// unique identifier set. Sequence set ranges are permitted.
/// </summary>
/// <param name="sequenceSet"></param>
/// <returns>It's self</returns>
public Criteria UID(string sequenceSet) { g_Crit = "UID " + sequenceSet; return this; }
/// <summary>
/// Messages that do not have the \Answered flag set.
/// </summary>
/// <returns>It's self</returns>
public Criteria UNANSWERED() { g_Crit = "UNANSWERED"; return this; }
/// <summary>
/// Messages that do not have the \Deleted flag set.
/// </summary>
/// <returns>It's self</returns>
public Criteria UNDELETED() { g_Crit = "UNDELETED"; return this; }
/// <summary>
/// Messages that do not have the \Draft flag set.
/// </summary>
/// <returns>It's self</returns>
public Criteria UNDRAFT() { g_Crit = "UNDRAFT"; return this; }
/// <summary>
/// Messages that do not have the \Flagged flag set.
/// </summary>
/// <returns>It's self</returns>
public Criteria UNFLAGGED() { g_Crit = "UNFLAGGED"; return this; }
/// <summary>
/// Messages that do not have the \Seen flag set.
/// </summary>
/// <returns>It's self</returns>
public Criteria UNSEEN() { g_Crit = "UNSEEN"; return this; }
/// <summary>
/// Messages that do not have the specified keyword flag set.
/// </summary>
/// <param name="flag"></param>
/// <returns>It's self</returns>
public Criteria UNKEYWORD(string flag) { g_Crit = "UNKEYWORD \"" + flag + "\""; return this; }
/// <summary>
/// Messages with the specified keyword flag set.
/// </summary>
/// <param name="flag"></param>
/// <returns>It's self</returns>
public Criteria KEYWORD(string flag) { g_Crit = "KEYWORD \"" + flag + "\""; return this; }
/// <summary>
/// Messages with an [RFC-2822] size smaller than the specified
/// number of octets.
/// </summary>
/// <param name="Size">Size in bytes</param>
/// <returns>It's self</returns>
public Criteria SMALLER(int Size) { g_Crit = "SMALLER " + Size.ToString(); return this; }
/// <summary>
/// Messages that contain the specified string in the envelope
/// structure's SUBJECT field.
/// </summary>
/// <param name="Text"></param>
/// <returns>It's self</returns>
public Criteria SUBJECT(string Text) { g_Crit = "SUBJECT \"" + Text + "\""; return this; }
/// <summary>
/// Messages that have a header with the specified field-name (as
/// defined in [RFC-2822]) and that contains the specified string
/// in the text of the header (what comes after the colon). If the
/// string to search is zero-length, this matches all messages that
/// have a header line with the specified field-name regardless of
/// the contents.
/// </summary>
/// <param name="fieldName">Header field name(before the colon)</param>
/// <param name="Text">Header value(after the colon)</param>
/// <returns>It's self</returns>
public Criteria HEADER(string fieldName, string Text){ g_Crit = "HEADER \"" + fieldName + "\" \"" + Text + "\""; return this; }
/// <summary>
/// Messages whose internal date (disregarding time and timezone)
/// is earlier than the specified date.
/// </summary>
/// <param name="Date">Search Date</param>
/// <returns>It's self</returns>
public Criteria BEFORE(DateTime Date)
{
g_Crit = "BEFORE \"" + Date.ToString("d-MMM-yyyy") + "\"";
return this;
}
/// <summary>
/// Messages whose internal date (disregarding time and timezone)
/// is within the specified date.
/// </summary>
/// <param name="Date">Search Date</param>
/// <returns>It's self</returns>
public Criteria ON(DateTime Date)
{
g_Crit = "ON \"" + Date.ToString("d-MMM-yyyy") + "\"";
return this;
}
/// <summary>
/// Messages whose [RFC-2822] Date: header (disregarding time and
/// timezone) is earlier than the specified date.
/// </summary>
/// <param name="Date">Search Date</param>
/// <returns>It's self</returns>
public Criteria SENTBEFORE(DateTime Date)
{
g_Crit = "SENTBEFORE \"" + Date.ToString("d-MMM-yyyy") + "\"";
return this;
}
/// <summary>
/// Messages whose [RFC-2822] Date: header (disregarding time and
/// timezone) is within the specified date.
/// </summary>
/// <param name="Date">Search Date</param>
/// <returns>It's self</returns>
public Criteria SENTON(DateTime Date)
{
g_Crit = "SENTON \"" + Date.ToString("d-MMM-yyyy") + "\"";
return this;
}
/// <summary>
/// Messages whose [RFC-2822] Date: header (disregarding time and
/// timezone) is within or later than the specified date.
/// </summary>
/// <param name="Date">Search Date</param>
/// <returns>It's self</returns>
public Criteria SENTSINCE(DateTime Date)
{
g_Crit = "SENTSINCE \"" + Date.ToString("d-MMM-yyyy") + "\"";
return this;
}
/// <summary>
/// Messages whose internal date (disregarding time and timezone)
/// is within or later than the specified date.
/// </summary>
/// <param name="Date">Search Date</param>
/// <returns>It's self</returns>
public Criteria SINCE(DateTime Date)
{
g_Crit = "SINCE \"" + Date.ToString("d-MMM-yyyy") + "\"";
return this;
}

#endregion

}

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 
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 

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.