Click here to Skip to main content
15,885,546 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 library threadsafe? Pin
jongerik28-Apr-08 21:58
jongerik28-Apr-08 21:58 
AnswerRe: Is library threadsafe? Pin
jongerik6-May-08 2:10
jongerik6-May-08 2:10 
Generalsmall bug in IMAP Client library Pin
eugeneshklyar17-Oct-07 7:14
eugeneshklyar17-Oct-07 7:14 
GeneralSearch Commands Pin
AlexeiBauer9-Aug-07 13:11
AlexeiBauer9-Aug-07 13:11 
GeneralRe: Search Commands Pin
Rohit Joshi10-Aug-07 3:58
Rohit Joshi10-Aug-07 3:58 
Questionhow do i access shared mailboxes? Pin
viralypatel22-Jul-07 10:59
viralypatel22-Jul-07 10:59 
AnswerRe: how do i access shared mailboxes? Pin
onlyMylove3-Mar-08 23:47
onlyMylove3-Mar-08 23:47 
Generaldelete mailbox Pin
oldcounty29-Jun-07 4:10
oldcounty29-Jun-07 4:10 
how to delete mailbox completely of terminated user

thanks
GeneralIssue with Login method Pin
niemkhuccuoi07015-Jun-07 5:31
niemkhuccuoi07015-Jun-07 5:31 
GeneralRe: Issue with Login method Pin
Rohit Joshi6-Aug-07 6:36
Rohit Joshi6-Aug-07 6:36 
GeneralSaving the Attachments into a path Pin
caubai1074-Jun-07 12:56
caubai1074-Jun-07 12:56 
GeneralRe: Saving the Attachments into a path Pin
Rutger Hauer18-Dec-07 23:15
Rutger Hauer18-Dec-07 23:15 
GeneralRe: Saving the Attachments into a path Pin
K32110-Feb-10 4:37
K32110-Feb-10 4:37 
QuestionHow do I delete a mail? Pin
caubai10722-May-07 4:50
caubai10722-May-07 4:50 
AnswerRe: How do I delete a mail? Pin
Rohit Joshi22-May-07 5:04
Rohit Joshi22-May-07 5:04 
QuestionRe: How do I delete a mail? Pin
caubai1074-Jun-07 12:44
caubai1074-Jun-07 12:44 
Generalsearch not returning Pin
MrOWNZ7-May-07 9:24
MrOWNZ7-May-07 9:24 
GeneralRe: search not returning Pin
Rohit Joshi7-May-07 9:39
Rohit Joshi7-May-07 9:39 
GeneralRe: search not returning Pin
MrOWNZ7-May-07 10:01
MrOWNZ7-May-07 10:01 
GeneralRe: search not returning Pin
MrOWNZ9-May-07 6:08
MrOWNZ9-May-07 6:08 
GeneralRe: search not returning Pin
MrOWNZ24-May-07 6:52
MrOWNZ24-May-07 6:52 
GeneralGroupwise Problem Pin
tpurves9-Apr-07 12:48
tpurves9-Apr-07 12:48 
GeneralExchange Server V6.5 Compatibility Pin
mschelstrate27-Mar-07 5:45
mschelstrate27-Mar-07 5:45 
GeneralRe: Exchange Server V6.5 Compatibility Pin
jongerik30-Nov-07 2:37
jongerik30-Nov-07 2:37 
GeneralFetchMessage Pin
Nitin198124-Mar-07 4:39
Nitin198124-Mar-07 4:39 

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.