Click here to Skip to main content
11,923,517 members (65,738 online)
Click here to Skip to main content
Add your own
alternative version


179 bookmarked

IMAP Client library using C#

, 20 Sep 2012 MPL
Rate this:
Please Sign up or sign in to vote.
IMAPLibrary supports the basic IMAP protocol functions to fetch messages from the mailbox.


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.
    • Logout: Logout and close the socket.
    • SelectFolder: It selects the folder. It requires folder name as parameter.
    • ExamineFolder: It is similar to SelectFolder, but it does examine.
    • GetQuota: Get the quota of the mailbox.
    • SearchMessage: You can search the messages. It will return the UID of messages. E.g., From rjoshi.
    • 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.
    • 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


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


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


About the Author

Rohit Joshi
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.

You may also be interested in...

Comments and Discussions

AnswerRe: GMail problem, only for GoogleApp account Pin
roberto.roncato11-Dec-12 22:38
memberroberto.roncato11-Dec-12 22:38 
GeneralRe: GMail problem, only for GoogleApp account Pin
Rohit Joshi24-Jan-13 5:15
memberRohit Joshi24-Jan-13 5:15 
QuestionSearching email subject vlues Pin
thetra30-Nov-12 4:43
memberthetra30-Nov-12 4:43 
AnswerRe: Searching email subject vlues Pin
Rohit Joshi30-Nov-12 7:30
memberRohit Joshi30-Nov-12 7:30 
GeneralRe: Searching email subject vlues Pin
thetra5-Dec-12 23:20
memberthetra5-Dec-12 23:20 
GeneralRe: Searching email subject vlues Pin
Rohit Joshi24-Jan-13 5:17
memberRohit Joshi24-Jan-13 5:17 
QuestionHow to use SearchMessage Pin
Christian A Berczely26-Oct-12 8:27
memberChristian A Berczely26-Oct-12 8:27 
AnswerRe: How to use SearchMessage Pin
Rohit Joshi29-Oct-12 11:08
memberRohit Joshi29-Oct-12 11:08 
See the below commands supported for IMAP protocol for search
FROM "<mailaddress>"	 Mail from that sender	FROM ""
TO "<mailaddress>"	 Mail to that recipient	TO ""
SINCE <date>	 Mail received after this date	SINCE 1-Nov-2009
BEFORE <date>	 Mail received before this date	BEFORE 1-Nov-2009
DELETED	 Mails marked as deleted	DELETED
SUBJECT <string>	 Mails containing string in the subject	SUBJECT "Proposal"
BODY <string>	 Mails containing string in the body	BODY "Hello Greg"
NOT <key>	 Mails which do not match the key	NOT FROM ""
OR <key1> <key2>	 Mails which match either of key1 or key2	OR FROM "" FROM ""

QuestionAdd mail to sent items Pin
RenoMarseille20-Oct-12 0:40
memberRenoMarseille20-Oct-12 0:40 
QuestionSTORE command failure Pin
Michael Mak16-Oct-12 19:10
memberMichael Mak16-Oct-12 19:10 
AnswerRe: STORE command failure Pin
Rohit Joshi17-Oct-12 5:03
memberRohit Joshi17-Oct-12 5:03 
GeneralRe: STORE command failure Pin
Michael Mak17-Oct-12 13:00
memberMichael Mak17-Oct-12 13:00 
QuestionFailure bodystructure command Pin
Member 800245416-Oct-12 6:44
memberMember 800245416-Oct-12 6:44 
AnswerRe: Failure bodystructure command Pin
Vibin Valsalan16-Sep-15 10:00
memberVibin Valsalan16-Sep-15 10:00 
QuestionFailure fetching message from IMAP folder/mailbox. BODY[HEADER] Pin
Member 800245416-Oct-12 6:30
memberMember 800245416-Oct-12 6:30 
QuestionFailure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Member 800245416-Oct-12 6:24
memberMember 800245416-Oct-12 6:24 
AnswerRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
PooranPrasad30-May-13 23:07
memberPooranPrasad30-May-13 23:07 
GeneralRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Javier Suero3-Jul-13 3:43
memberJavier Suero3-Jul-13 3:43 
GeneralRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Member 800245418-Mar-14 1:26
memberMember 800245418-Mar-14 1:26 
AnswerRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Rohit Joshi8-Jun-13 16:51
memberRohit Joshi8-Jun-13 16:51 
GeneralRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Javier Suero3-Jul-13 7:35
memberJavier Suero3-Jul-13 7:35 
QuestionCan't obtain email body Pin
eladfrn16-Oct-12 5:46
membereladfrn16-Oct-12 5:46 
GeneralMessage Removed Pin
rk.tedlapu8-Oct-12 3:19
memberrk.tedlapu8-Oct-12 3:19 
GeneralRe: My vote of 4 Pin
Rohit Joshi8-Oct-12 4:44
memberRohit Joshi8-Oct-12 4:44 
QuestionHow to save the mail in "sent items" folder Pin
rk.tedlapu8-Oct-12 3:14
memberrk.tedlapu8-Oct-12 3:14 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151125.3 | Last Updated 20 Sep 2012
Article Copyright 2004 by Rohit Joshi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid