Click here to Skip to main content
15,897,291 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: Is there sample code to download attachment? Pin
jymitra30-Jul-10 1:51
jymitra30-Jul-10 1:51 
GeneralSpecial chars Pin
Lucas Santos Sanches15-Jul-09 14:41
Lucas Santos Sanches15-Jul-09 14:41 
GeneralMessage Closed Pin
14-Oct-09 11:45
lesnikowski14-Oct-09 11:45 
GeneralRe: Special chars Pin
K32122-Mar-10 0:17
K32122-Mar-10 0:17 
GeneralMark read in SELECT and EXAMINE Pin
Lucas Santos Sanches8-Jul-09 13:58
Lucas Santos Sanches8-Jul-09 13:58 
GeneralRe: Mark read in SELECT and EXAMINE Pin
Lucas Santos Sanches9-Jul-09 6:50
Lucas Santos Sanches9-Jul-09 6:50 
GeneralRe: Mark read in SELECT and EXAMINE Pin
Rohit Joshi9-Jul-09 11:36
Rohit Joshi9-Jul-09 11:36 
GeneralRe: Mark read in SELECT and EXAMINE Pin
Lucas Santos Sanches10-Jul-09 2:27
Lucas Santos Sanches10-Jul-09 2:27 
Thank for you reply Rohit.

Log? Where? I try and still mark as read...

I don't have VS2003 in my computer, I build and test in VS2005. But nothing strange ocorred in conversion, complete with sucess.
I downloaded again and only exec EXE. Same thing, mark as read...


I found another vs solution (named with ImapLibrary) in my computer (realy,I do not know how to come on my computer lol), but this is with SLL. You know something??
GeneralRe: Mark read in SELECT and EXAMINE Pin
Rohit Joshi10-Jul-09 3:16
Rohit Joshi10-Jul-09 3:16 
GeneralRe: Mark read in SELECT and EXAMINE Pin
Lucas Santos Sanches10-Jul-09 3:55
Lucas Santos Sanches10-Jul-09 3:55 
GeneralRe: Mark read in SELECT and EXAMINE Pin
Rohit Joshi10-Jul-09 5:18
Rohit Joshi10-Jul-09 5:18 
GeneralRe: Mark read in SELECT and EXAMINE Pin
Lucas Santos Sanches10-Jul-09 6:09
Lucas Santos Sanches10-Jul-09 6:09 
GeneralRe: Mark read in SELECT and EXAMINE Pin
Rohit Joshi10-Jul-09 7:30
Rohit Joshi10-Jul-09 7:30 
QuestionNot connected to gmail Pin
ashishshar11-May-09 23:12
ashishshar11-May-09 23:12 
AnswerRe: Not connected to gmail Pin
10BRG26-Aug-09 3:04
10BRG26-Aug-09 3:04 
QuestionWhat is syntax for expunge uid? Pin
manchu7311-May-09 6:45
manchu7311-May-09 6:45 
AnswerRe: What is syntax for expunge uid? Pin
Rohit Joshi11-May-09 7:48
Rohit Joshi11-May-09 7:48 
GeneralRe: What is syntax for expunge uid? Pin
manchu7311-May-09 7:52
manchu7311-May-09 7:52 
QuestionHow to Use this library Pin
CMC12315-Apr-09 4:47
CMC12315-Apr-09 4:47 
AnswerRe: How to Use this library Pin
newbish16-Apr-09 16:32
newbish16-Apr-09 16:32 
QuestionWhat is the UID for this Message that I can call Fetch with? Pin
Francis Lau9-Apr-09 14:43
Francis Lau9-Apr-09 14:43 
AnswerRe: What is the UID for this Message that I can call Fetch with? Pin
Rohit Joshi11-May-09 7:50
Rohit Joshi11-May-09 7:50 
AnswerMessage Closed Pin
14-Oct-09 11:53
lesnikowski14-Oct-09 11:53 
QuestionProblems downloading messages with cyrus imap 2.2.13-14+b3 Pin
pcalder0n18-Mar-09 16:35
pcalder0n18-Mar-09 16:35 
AnswerRe: Problems downloading messages with cyrus imap 2.2.13-14+b3 Pin
Rohit Joshi24-Mar-09 5:40
Rohit Joshi24-Mar-09 5:40 

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.