Click here to Skip to main content
11,496,146 members (640 online)
Click here to Skip to main content

IMAP Client library using C#

, 20 Sep 2012 MPL 718.2K 22.7K 174
IMAPLibrary supports the basic IMAP protocol functions to fetch messages from the mailbox.
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

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

   
 

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 !!

License

This article, along with any associated source code and files, is licensed under The Mozilla Public License 1.1 (MPL 1.1)

Share

About the Author

Rohit Joshi
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: Gmail Using the new SSL Code Pin
Member 319211329-Dec-08 21:32
memberMember 319211329-Dec-08 21:32 
Generalnotepad attachement Pin
joekey14-Apr-08 23:59
memberjoekey14-Apr-08 23:59 
GeneralRe: notepad attachement Pin
Rohit Joshi15-Apr-08 4:26
memberRohit Joshi15-Apr-08 4:26 
GeneralRe: notepad attachement Pin
joekey15-Apr-08 20:48
memberjoekey15-Apr-08 20:48 
GeneralRe: notepad attachement Pin
joekey18-Apr-08 1:45
memberjoekey18-Apr-08 1:45 
GeneralRe: notepad attachement Pin
slymenk21-Apr-08 1:47
memberslymenk21-Apr-08 1:47 
GeneralRe: notepad attachement [modified] Pin
Rohit Joshi21-Apr-08 14:17
memberRohit Joshi21-Apr-08 14:17 
GeneralRe: notepad attachement Pin
joekey6-May-08 1:05
memberjoekey6-May-08 1:05 
GeneralRe: notepad attachement Pin
jymitra28-Jul-10 5:00
memberjymitra28-Jul-10 5:00 
QuestionMove a message into a folder? Pin
tbrewer11-Apr-08 12:01
membertbrewer11-Apr-08 12:01 
AnswerRe: Move a message into a folder? Pin
Atmospherian11-Apr-08 12:54
memberAtmospherian11-Apr-08 12:54 
GeneralRe: Move a message into a folder? Pin
garryihill5-Oct-11 5:12
membergarryihill5-Oct-11 5:12 
NewsNew version of library coming soon Pin
Atmospherian11-Apr-08 1:46
memberAtmospherian11-Apr-08 1:46 
GeneralThe future of this library, Rohit, please respond Pin
Atmospherian8-Apr-08 5:55
memberAtmospherian8-Apr-08 5:55 
Hi Rohit,

I understand that you undoubtedly have other commitments that keep you from updating this library, and myself and a few others have expressed interest in seeing some bugs fixed and new features implemented.

I would like to volunteer to take over the development and maintenance of your library, with your kind permission. the library will remain free and open-source and you will be credited as the original author in all documentation and source code that is released.

I have already added support for SSL and retrieving the list of folders, and others have expressed interest in getting a copy of the updated code, but i wanted to check with you and make sure you are ok with updated versions of your code being released.

If you consent, i will be creating a codeplex project for this library and will be sure to link back to your article.

You can reach me at atmospherian at gmail.com

Thanks for your consideration,
Jason Miesionczek
GeneralRe: The future of this library, Rohit, please respond Pin
Rohit Joshi9-Apr-08 4:50
memberRohit Joshi9-Apr-08 4:50 
QuestionWorks with gmail? Pin
b_raja7-Apr-08 6:26
memberb_raja7-Apr-08 6:26 
GeneralRe: Works with gmail? Pin
Atmospherian7-Apr-08 7:10
memberAtmospherian7-Apr-08 7:10 
AnswerRe: Works with gmail? Pin
sekhar vikram29-May-08 6:30
membersekhar vikram29-May-08 6:30 
GeneralAdded SSL support Pin
Atmospherian7-Apr-08 5:24
memberAtmospherian7-Apr-08 5:24 
GeneralRe: Added SSL support Pin
Member 32792227-Mar-10 23:46
memberMember 32792227-Mar-10 23:46 
GeneralSelecting Sent Items folder using Select command Pin
Sivasankari Jayaraj27-Mar-08 3:05
memberSivasankari Jayaraj27-Mar-08 3:05 
GeneralRe: Selecting Sent Items folder using Select command Pin
Atmospherian23-Apr-08 16:51
memberAtmospherian23-Apr-08 16:51 
GeneralRe: Selecting Sent Items folder using Select command Pin
Member 473069915-Dec-08 23:52
memberMember 473069915-Dec-08 23:52 
QuestionList of Folders? Pin
Catalin MAICAN10-Mar-08 4:09
memberCatalin MAICAN10-Mar-08 4:09 
AnswerRe: List of Folders? Pin
Atmospherian7-Apr-08 16:36
memberAtmospherian7-Apr-08 16:36 
GeneralRe: List of Folders? [modified] Pin
jymitra22-Jul-10 3:11
memberjymitra22-Jul-10 3:11 
GeneralRe: List of Folders? Pin
jymitra22-Jul-10 5:00
memberjymitra22-Jul-10 5:00 
Questionwhat is a Message UID Pin
Usans25-Feb-08 1:22
memberUsans25-Feb-08 1:22 
GeneralRe: what is a Message UID Pin
Rohit Joshi28-Feb-08 14:31
memberRohit Joshi28-Feb-08 14:31 
GeneralReceiveBuffer Pin
Anon1234514-Dec-07 4:16
sussAnon1234514-Dec-07 4:16 
GeneralRe: ReceiveBuffer Pin
eugeneshklyar14-Dec-07 11:56
membereugeneshklyar14-Dec-07 11:56 
QuestionExtract Custom Headers [modified] Pin
sad_721-Nov-07 7:06
membersad_721-Nov-07 7:06 
QuestionIs library threadsafe? Pin
sternentanz23-Oct-07 3:37
membersternentanz23-Oct-07 3:37 
GeneralRe: Is library threadsafe? Pin
jongerik28-Apr-08 22:58
memberjongerik28-Apr-08 22:58 
AnswerRe: Is library threadsafe? Pin
jongerik6-May-08 3:10
memberjongerik6-May-08 3:10 
Generalsmall bug in IMAP Client library Pin
eugeneshklyar17-Oct-07 8:14
membereugeneshklyar17-Oct-07 8:14 
GeneralSearch Commands Pin
AlexeiBauer9-Aug-07 14:11
memberAlexeiBauer9-Aug-07 14:11 
GeneralRe: Search Commands Pin
Rohit Joshi10-Aug-07 4:58
memberRohit Joshi10-Aug-07 4:58 
Questionhow do i access shared mailboxes? Pin
viralypatel22-Jul-07 11:59
memberviralypatel22-Jul-07 11:59 
AnswerRe: how do i access shared mailboxes? Pin
onlyMylove4-Mar-08 0:47
memberonlyMylove4-Mar-08 0:47 
Generaldelete mailbox Pin
oldcounty29-Jun-07 5:10
memberoldcounty29-Jun-07 5:10 
GeneralIssue with Login method Pin
ibgvfdfyoroqrf5-Jun-07 6:31
memberibgvfdfyoroqrf5-Jun-07 6:31 
GeneralRe: Issue with Login method Pin
Rohit Joshi6-Aug-07 7:36
memberRohit Joshi6-Aug-07 7:36 
GeneralSaving the Attachments into a path Pin
caubai1074-Jun-07 13:56
membercaubai1074-Jun-07 13:56 
GeneralRe: Saving the Attachments into a path Pin
Rutger Hauer19-Dec-07 0:15
memberRutger Hauer19-Dec-07 0:15 
GeneralRe: Saving the Attachments into a path Pin
K32110-Feb-10 5:37
memberK32110-Feb-10 5:37 
QuestionHow do I delete a mail? Pin
caubai10722-May-07 5:50
membercaubai10722-May-07 5:50 
AnswerRe: How do I delete a mail? Pin
Rohit Joshi22-May-07 6:04
memberRohit Joshi22-May-07 6:04 
QuestionRe: How do I delete a mail? Pin
caubai1074-Jun-07 13:44
membercaubai1074-Jun-07 13:44 
Generalsearch not returning Pin
MrOWNZ7-May-07 10:24
memberMrOWNZ7-May-07 10:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 20 Sep 2012
Article Copyright 2004 by Rohit Joshi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid