Click here to Skip to main content
15,898,134 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: extract E-mail header and store it into database Pin
Rohit Joshi23-Jan-05 12:07
Rohit Joshi23-Jan-05 12:07 
GeneralRe: extract E-mail header and store it into database Pin
Rohit Joshi23-Jan-05 12:14
Rohit Joshi23-Jan-05 12:14 
GeneralRe: extract E-mail header and store it into database Pin
little_duck26-Jan-05 6:48
little_duck26-Jan-05 6:48 
GeneralRe: extract E-mail header and store it into database Pin
Rohit Joshi27-Jan-05 8:10
Rohit Joshi27-Jan-05 8:10 
Generalvery , very nice... Pin
pr1nce_goulash16-Dec-04 8:07
pr1nce_goulash16-Dec-04 8:07 
GeneralRe: very , very nice... Pin
Rohit Joshi20-Dec-04 5:12
Rohit Joshi20-Dec-04 5:12 
GeneralBodies and Attachmetns Pin
maraymer1-Oct-04 18:01
maraymer1-Oct-04 18:01 
GeneralRe: Bodies and Attachmetns Pin
Rohit Joshi2-Oct-04 16:41
Rohit Joshi2-Oct-04 16:41 
It does what you need.
It does fetch the body. When you set the flag to TRUE, it will generate the XML file with message UID as file name. E.g If message UID is 35 than it will create 35.xml from the location where you started the project or probably in C: drive. You can verify in ImapLibraryTest project.

If you are not familiar with the body structure of the attachments and header, you can refer MessageStructure.txt included in project which shows how headers and attachements are referred while fetching a message.

Let me explain you how message is fetch.
First we need to fetch the body structure of the message which shows what are the different part of the message including attachments are there in message.
This is done by two functions.
1. GetBodyStructure() which internally calls the
2. ParsebodyStructur()

Once body structur if parsed, following functions are used to fetch the message body and header by psecifying part number.
1. GetBody()
2. GetHeader()

Currently, it dumps everything into XML file. Either you can recreate message from the XML file or instead of creating XML file, you can save in your preferred format and recreate the attachments back based on content-type and encoding. If encoding is base64, you need to convert back to original format.

Let me know if you need furthure help.

I am planning to update this article in which explain about how IMAP protocol is implemented but not getting time to do soFrown | :(
GeneralRe: Bodies and Attachmetns Pin
maraymer4-Oct-04 19:38
maraymer4-Oct-04 19:38 
GeneralRe: Bodies and Attachmetns Pin
Rohit Joshi5-Oct-04 15:11
Rohit Joshi5-Oct-04 15:11 
GeneralRe: Bodies and Attachmetns Pin
maraymer8-Oct-04 17:00
maraymer8-Oct-04 17:00 
GeneralIMAP Client Pin
Anonymous30-Aug-04 11:15
Anonymous30-Aug-04 11:15 
GeneralRe: IMAP Client Pin
Rohit Joshi31-Aug-04 4:04
Rohit Joshi31-Aug-04 4:04 
GeneralRe: IMAP Client Pin
Anonymous27-Jan-05 0:55
Anonymous27-Jan-05 0:55 
GeneralHandy Pin
Brian Delahunty17-Aug-04 6:20
Brian Delahunty17-Aug-04 6:20 
GeneralRe: Handy Pin
Rohit Joshi18-Aug-04 5:50
Rohit Joshi18-Aug-04 5:50 
GeneralRe: Handy Pin
jadedgeek19-Aug-04 15:26
jadedgeek19-Aug-04 15:26 

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.