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

 
AnswerRe: About usage of library Pin
Rohit Joshi12-Oct-10 15:28
Rohit Joshi12-Oct-10 15:28 
AnswerRe: About usage of library [modified] Pin
Rohit Joshi6-Jun-12 10:41
Rohit Joshi6-Jun-12 10:41 
GeneralRe: About usage of library [modified] Pin
anheru15-Jun-12 3:34
anheru15-Jun-12 3:34 
QuestionHow to get body? Pin
Texxan13-Jul-10 23:13
Texxan13-Jul-10 23:13 
GeneralSSL conntection to imap.googlemail.com Pin
Semail16-Jun-10 7:48
Semail16-Jun-10 7:48 
GeneralRe: SSL conntection to imap.googlemail.com Pin
Rohit Joshi6-Jun-12 10:41
Rohit Joshi6-Jun-12 10:41 
GeneralCreate New Message Pin
Member 314484019-May-10 3:48
Member 314484019-May-10 3:48 
GeneralUnable to read some mail content Pin
Member 31448409-May-10 1:01
Member 31448409-May-10 1:01 
Hi
I used this liberary. Few mails contant are unable to read show like below.

PGh0bWwgeG1sbnM6dj0idXJuOnNjaGVtYXMtbWljcm9zb2Z0LWNvbTp2bWwiIHhtbG5zOm89InVy bjpzY2hlbWFzLW1p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ly9taWNyb3NvZnQuY29tL29mZmljZW5ldC9jb25mZXJlbmNpbmciIHhtbG5zOkQ9IkRB VjoiIHhtbG5zOlJlcGw9Imh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vcmVwbC8iIHhtbG5z Om10PSJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3NoYXJlcG9pbnQvc29hcC9tZWV0aW5n cy8iIHhtbG5zOngyPSJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL29mZmljZS9leGNlbC8y MDAzL3htbCIgeG1sbnM6cHBkYT0iaHR0cDovL3d3dy5wYXNzcG9ydC5jb20vTmFtZVNwYWNlLnhz

Any advice to solve this problem???
GeneralRe: Unable to read some mail content Pin
Rohit Joshi9-May-10 6:36
Rohit Joshi9-May-10 6:36 
GeneralRe: Unable to read some mail content Pin
armukesh15-May-10 23:44
armukesh15-May-10 23:44 
GeneralFETCH Message Header Without update FLAG Status Pin
Member 314484015-Apr-10 21:45
Member 314484015-Apr-10 21:45 
GeneralRe: FETCH Message Header Without update FLAG Status Pin
_PolyGram_17-Apr-10 23:33
_PolyGram_17-Apr-10 23:33 
GeneralRe: FETCH Message Header Without update FLAG Status Pin
Rohit Joshi9-May-10 6:41
Rohit Joshi9-May-10 6:41 
GeneralRe: FETCH Message Header Without update FLAG Status Pin
armukesh15-May-10 23:51
armukesh15-May-10 23:51 
GeneralAddendum to my reply below: Not to find more people spreading Myth about the GPL License! Pin
_Khallaf3-Apr-10 6:20
_Khallaf3-Apr-10 6:20 
GeneralRe: Addendum to my reply below: Not to find more people spreading Myth about the GPL License! Pin
Rohit Joshi13-Apr-10 16:13
Rohit Joshi13-Apr-10 16:13 
GeneralOne more thing... Pin
_Khallaf24-Mar-10 18:29
_Khallaf24-Mar-10 18:29 
GeneralExcellent, but... Pin
_Khallaf24-Mar-10 18:16
_Khallaf24-Mar-10 18:16 
GeneralYour's is GPL license which is useless for developers who wants to use the code Pin
Rohit Joshi2-Apr-10 16:34
Rohit Joshi2-Apr-10 16:34 
GeneralRe: Your's is GPL license which is useless for developers who wants to use the code Pin
_Khallaf3-Apr-10 5:48
_Khallaf3-Apr-10 5:48 
GeneralRe: Your's is GPL license which is useless for developers who wants to use the code Pin
Chris Maunder16-Jul-10 16:07
cofounderChris Maunder16-Jul-10 16:07 
GeneralMy vote of 1 Pin
legendicus3-Mar-10 8:16
legendicus3-Mar-10 8:16 
GeneralRe: My vote of 1 Pin
Rohit Joshi20-Mar-10 12:33
Rohit Joshi20-Mar-10 12:33 
QuestionHow to get mesaage body Pin
cmsn27-Feb-10 19:31
cmsn27-Feb-10 19:31 
AnswerRe: How to get mesaage body Pin
_PolyGram_24-Mar-10 17:59
_PolyGram_24-Mar-10 17:59 

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.