Click here to Skip to main content
13,592,003 members
Click here to Skip to main content
Add your own
alternative version


184 bookmarked
Posted 16 Aug 2004
Licenced MPL

IMAP Client library using C#

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

GitHub Link:



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 and PR welcome at github repository!!






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

GeneralRe: notepad attachement [modified] Pin
Rohit Joshi21-Apr-08 13:17
memberRohit Joshi21-Apr-08 13:17 
GeneralRe: notepad attachement Pin
joekey6-May-08 0:05
memberjoekey6-May-08 0:05 
GeneralRe: notepad attachement Pin
jymitra28-Jul-10 4:00
memberjymitra28-Jul-10 4:00 
QuestionMove a message into a folder? Pin
tbrewer11-Apr-08 11:01
membertbrewer11-Apr-08 11:01 
AnswerRe: Move a message into a folder? Pin
Atmospherian11-Apr-08 11:54
memberAtmospherian11-Apr-08 11:54 
GeneralRe: Move a message into a folder? Pin
garryihill5-Oct-11 4:12
membergarryihill5-Oct-11 4:12 
NewsNew version of library coming soon Pin
Atmospherian11-Apr-08 0:46
memberAtmospherian11-Apr-08 0:46 
GeneralThe future of this library, Rohit, please respond Pin
Atmospherian8-Apr-08 4:55
memberAtmospherian8-Apr-08 4:55 
Hi Rohit,

I understand that you undoubtedly have other commitments that keep you from updating this library, and myself and a few others have expressed interest in seeing some bugs fixed and new features implemented.

I would like to volunteer to take over the development and maintenance of your library, with your kind permission. the library will remain free and open-source and you will be credited as the original author in all documentation and source code that is released.

I have already added support for SSL and retrieving the list of folders, and others have expressed interest in getting a copy of the updated code, but i wanted to check with you and make sure you are ok with updated versions of your code being released.

If you consent, i will be creating a codeplex project for this library and will be sure to link back to your article.

You can reach me at atmospherian at

Thanks for your consideration,
Jason Miesionczek
GeneralRe: The future of this library, Rohit, please respond Pin
Rohit Joshi9-Apr-08 3:50
memberRohit Joshi9-Apr-08 3:50 
QuestionWorks with gmail? Pin
b_raja7-Apr-08 5:26
memberb_raja7-Apr-08 5:26 
GeneralRe: Works with gmail? Pin
Atmospherian7-Apr-08 6:10
memberAtmospherian7-Apr-08 6:10 
AnswerRe: Works with gmail? Pin
sekhar vikram29-May-08 5:30
membersekhar vikram29-May-08 5:30 
GeneralAdded SSL support Pin
Atmospherian7-Apr-08 4:24
memberAtmospherian7-Apr-08 4:24 
GeneralRe: Added SSL support Pin
Member 32792227-Mar-10 22:46
memberMember 32792227-Mar-10 22:46 
GeneralSelecting Sent Items folder using Select command Pin
Sivasankari Jayaraj27-Mar-08 2:05
memberSivasankari Jayaraj27-Mar-08 2:05 
GeneralRe: Selecting Sent Items folder using Select command Pin
Atmospherian23-Apr-08 15:51
memberAtmospherian23-Apr-08 15:51 
GeneralRe: Selecting Sent Items folder using Select command Pin
Member 473069915-Dec-08 22:52
memberMember 473069915-Dec-08 22:52 
QuestionList of Folders? Pin
Catalin MAICAN10-Mar-08 3:09
memberCatalin MAICAN10-Mar-08 3:09 
AnswerRe: List of Folders? Pin
Atmospherian7-Apr-08 15:36
memberAtmospherian7-Apr-08 15:36 
GeneralRe: List of Folders? [modified] Pin
jymitra22-Jul-10 2:11
memberjymitra22-Jul-10 2:11 
GeneralRe: List of Folders? Pin
jymitra22-Jul-10 4:00
memberjymitra22-Jul-10 4:00 
Questionwhat is a Message UID Pin
Usans25-Feb-08 0:22
memberUsans25-Feb-08 0:22 
GeneralRe: what is a Message UID Pin
Rohit Joshi28-Feb-08 13:31
memberRohit Joshi28-Feb-08 13:31 
GeneralReceiveBuffer Pin
Anon1234514-Dec-07 3:16
sussAnon1234514-Dec-07 3:16 
GeneralRe: ReceiveBuffer Pin
eugeneshklyar14-Dec-07 10:56
membereugeneshklyar14-Dec-07 10:56 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.180618.1 | Last Updated 20 Sep 2012
Article Copyright 2004 by Rohit Joshi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid