Click here to Skip to main content
15,357,358 members
Articles / Web Development / HTML
Posted 16 Aug 2004


184 bookmarked

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

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






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.

Comments and Discussions

BugThe program does not fetch full size of mail body Pin
mf_25-Dec-20 11:29
Membermf_25-Dec-20 11:29 
GeneralRe: The program does not fetch full size of mail body Pin
mf_212-Dec-20 9:35
Membermf_212-Dec-20 9:35 
QuestionHow to get number of messages? Pin
Sigurd Ottomann16-Jul-19 22:02
MemberSigurd Ottomann16-Jul-19 22:02 
QuestionCannot connect to gmail server. What am I missing? Pin
MadRushs9-May-19 2:44
MemberMadRushs9-May-19 2:44 
QuestionImap Library / How make Imap Idle function ? Pin
Member 1302687610-Oct-18 8:20
MemberMember 1302687610-Oct-18 8:20 
QuestionHow do I get the UIDs and the subject, body, from / to attributes etc.. of the emails Pin
montecristoh18-Feb-18 23:02
Membermontecristoh18-Feb-18 23:02 
QuestionGettingAlwaysTotalMailboxquotasize Pin
Member 1333518318-Jan-18 21:22
MemberMember 1333518318-Jan-18 21:22 
QuestionNew mail alert Pin
Kalpesh Shah10-Oct-17 22:34
MemberKalpesh Shah10-Oct-17 22:34 
QuestionPutting EXHIBIT A -Mozilla Public License in all copies of my software? Pin
aazam rafiee zade30-Aug-16 1:05
Memberaazam rafiee zade30-Aug-16 1:05 
QuestionCongratulations. How to get todays emails? Pin
Member 118840547-Mar-16 0:07
MemberMember 118840547-Mar-16 0:07 
Questionhow to use it? I'm beginner Pin
coder77127-Jan-16 2:10
professionalcoder77127-Jan-16 2:10 
AnswerRe: how to use it? I'm beginner Pin
Garth J Lancaster27-Jan-16 2:43
professionalGarth J Lancaster27-Jan-16 2:43 
GeneralRe: how to use it? I'm beginner Pin
coder77127-Jan-16 4:43
professionalcoder77127-Jan-16 4:43 
QuestionError while connecting to server Pin
Navin Pandit16-Jan-16 22:08
MemberNavin Pandit16-Jan-16 22:08 
AnswerRe: Error while connecting to server Pin
AJ_Smit28-Mar-16 22:09
MemberAJ_Smit28-Mar-16 22:09 
Questionmore info on question below Pin
Member 1201959512-Oct-15 0:26
MemberMember 1201959512-Oct-15 0:26 
Questionemail with attachement but no body Pin
Member 120195959-Oct-15 1:54
MemberMember 120195959-Oct-15 1:54 
QuestionSaving Attachment from the XML file Pin
Vibin Valsalan16-Sep-15 9:16
MemberVibin Valsalan16-Sep-15 9:16 
QuestionHow could I get the attachment? Pin
Dolby.Xiaowen24-Jul-15 15:15
MemberDolby.Xiaowen24-Jul-15 15:15 
AnswerRe: How could I get the attachment? Pin
Rohit Joshi4-Sep-15 4:47
MemberRohit Joshi4-Sep-15 4:47 
QuestionRe: How could I get the attachment? Pin
Member 121955619-Dec-15 1:44
MemberMember 121955619-Dec-15 1:44 
QuestionIs it possible to use the source code in commercial applications Pin
Member 116659175-May-15 3:15
MemberMember 116659175-May-15 3:15 
AnswerRe: Is it possible to use the source code in commercial applications Pin
Rohit Joshi4-Sep-15 4:35
MemberRohit Joshi4-Sep-15 4:35 
BugBug with large attachments Pin
Alexey T9-Mar-15 20:31
MemberAlexey T9-Mar-15 20:31 
GeneralRe: Bug with large attachments Pin
Rohit Joshi4-Sep-15 4:46
MemberRohit Joshi4-Sep-15 4:46 

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.