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

 
QuestionGMail problem, only for GoogleApp account Pin
roberto.roncato11-Dec-12 21:24
roberto.roncato11-Dec-12 21:24 
AnswerRe: GMail problem, only for GoogleApp account Pin
roberto.roncato11-Dec-12 21:38
roberto.roncato11-Dec-12 21:38 
GeneralRe: GMail problem, only for GoogleApp account Pin
Rohit Joshi24-Jan-13 4:15
Rohit Joshi24-Jan-13 4:15 
QuestionSearching email subject vlues Pin
thetra30-Nov-12 3:43
thetra30-Nov-12 3:43 
AnswerRe: Searching email subject vlues Pin
Rohit Joshi30-Nov-12 6:30
Rohit Joshi30-Nov-12 6:30 
GeneralRe: Searching email subject vlues Pin
thetra5-Dec-12 22:20
thetra5-Dec-12 22:20 
GeneralRe: Searching email subject vlues Pin
Rohit Joshi24-Jan-13 4:17
Rohit Joshi24-Jan-13 4:17 
QuestionHow to use SearchMessage Pin
Christian A Berczely26-Oct-12 7:27
Christian A Berczely26-Oct-12 7:27 
AnswerRe: How to use SearchMessage Pin
Rohit Joshi29-Oct-12 10:08
Rohit Joshi29-Oct-12 10:08 
QuestionAdd mail to sent items Pin
RenoMarseille19-Oct-12 23:40
RenoMarseille19-Oct-12 23:40 
QuestionSTORE command failure Pin
Michael Mak16-Oct-12 18:10
Michael Mak16-Oct-12 18:10 
AnswerRe: STORE command failure Pin
Rohit Joshi17-Oct-12 4:03
Rohit Joshi17-Oct-12 4:03 
GeneralRe: STORE command failure Pin
Michael Mak17-Oct-12 12:00
Michael Mak17-Oct-12 12:00 
QuestionFailure bodystructure command Pin
Marwa Trust Mutemasango16-Oct-12 5:44
Marwa Trust Mutemasango16-Oct-12 5:44 
AnswerRe: Failure bodystructure command Pin
Vibin Valsalan16-Sep-15 9:00
Vibin Valsalan16-Sep-15 9:00 
QuestionFailure fetching message from IMAP folder/mailbox. BODY[HEADER] Pin
Marwa Trust Mutemasango16-Oct-12 5:30
Marwa Trust Mutemasango16-Oct-12 5:30 
QuestionFailure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Marwa Trust Mutemasango16-Oct-12 5:24
Marwa Trust Mutemasango16-Oct-12 5:24 
AnswerRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
PooranPrasad30-May-13 22:07
PooranPrasad30-May-13 22:07 
GeneralRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Javier Suero3-Jul-13 2:43
Javier Suero3-Jul-13 2:43 
GeneralRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Marwa Trust Mutemasango18-Mar-14 0:26
Marwa Trust Mutemasango18-Mar-14 0:26 
AnswerRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Rohit Joshi8-Jun-13 15:51
Rohit Joshi8-Jun-13 15:51 
GeneralRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Javier Suero3-Jul-13 6:35
Javier Suero3-Jul-13 6:35 
QuestionCan't obtain email body Pin
eladfrn16-Oct-12 4:46
eladfrn16-Oct-12 4:46 
GeneralMessage Closed Pin
8-Oct-12 2:19
rk.tedlapu8-Oct-12 2:19 
GeneralRe: My vote of 4 Pin
Rohit Joshi8-Oct-12 3:44
Rohit Joshi8-Oct-12 3:44 

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.