Click here to Skip to main content
11,645,889 members (67,516 online)
Click here to Skip to main content

IMAP Client library using C#

, 20 Sep 2012 MPL 730.7K 23.3K 176
Rate this:
Please Sign up or sign in to vote.
IMAPLibrary supports the basic IMAP protocol functions to fetch messages from the mailbox.

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.

You may also be interested in...

Comments and Discussions

 
QuestionHow do you retreive attachments? Pin
Dan Guzman14-Sep-11 12:36
memberDan Guzman14-Sep-11 12:36 
AnswerRe: How do you retreive attachments? Pin
Rohit Joshi4-Apr-12 4:00
memberRohit Joshi4-Apr-12 4:00 
GeneralRe: How do you retreive attachments? Pin
vishal.suthar21-Jul-12 3:01
membervishal.suthar21-Jul-12 3:01 
GeneralCannot connect to Gmail IMAP on Port 993 Pin
Kmistry16-Jun-11 11:09
memberKmistry16-Jun-11 11:09 
GeneralRe: Cannot connect to Gmail IMAP on Port 993 Pin
Syed Majid Ali Gardezi10-Oct-11 1:35
memberSyed Majid Ali Gardezi10-Oct-11 1:35 
QuestionGetting Email size Pin
RamaNeni13-May-11 0:17
memberRamaNeni13-May-11 0:17 
AnswerRe: Getting Email size Pin
Rohit Joshi7-Jun-12 7:54
memberRohit Joshi7-Jun-12 7:54 
GeneralError: Failure connecting to the IMAP server. imap.gmail.com Pin
nyeinchan.ucsm4-May-11 17:26
membernyeinchan.ucsm4-May-11 17:26 
Hi All,

I downloaded IMAP Client library and trying to test with Host: imap.gmail.com , User:...., Password:.... . But I got an error like "Error: Failure connecting to the IMAP server. imap.gmail.com". I trying to connect with port 993, but I got the same error. I can't login to IMAP server.Please give me any advice.

Thanks and Regards
GeneralRe: Error: Failure connecting to the IMAP server. imap.gmail.com Pin
AnushaRose12-Apr-12 0:08
memberAnushaRose12-Apr-12 0:08 
GeneralRe: Error: Failure connecting to the IMAP server. imap.gmail.com Pin
Rohit Joshi6-Jun-12 10:09
memberRohit Joshi6-Jun-12 10:09 
GeneralCommand "UID SEARCH FROM" in Exchange 2010 Pin
Member 769955225-Feb-11 5:21
memberMember 769955225-Feb-11 5:21 
GeneralDid SSL ever get added Pin
Scott W Bowyer24-Feb-11 12:27
memberScott W Bowyer24-Feb-11 12:27 
GeneralRe: Did SSL ever get added Pin
Dhaidh30-May-12 5:52
memberDhaidh30-May-12 5:52 
GeneralRe: Did SSL ever get added Pin
Rohit Joshi6-Jun-12 10:36
memberRohit Joshi6-Jun-12 10:36 
GeneralRe: Did SSL ever get added Pin
Rohit Joshi6-Jun-12 10:37
memberRohit Joshi6-Jun-12 10:37 
GeneralNewbie Q, i dont understand something Pin
Gothrek21-Feb-11 1:12
memberGothrek21-Feb-11 1:12 
GeneralRe: Newbie Q, i dont understand something Pin
Rohit Joshi4-Apr-12 4:02
memberRohit Joshi4-Apr-12 4:02 
GeneralRe: Newbie Q, i dont understand something Pin
Rohit Joshi6-Jun-12 10:10
memberRohit Joshi6-Jun-12 10:10 
QuestionGetting Wrong Response...help Pin
jymitra28-Dec-10 23:32
memberjymitra28-Dec-10 23:32 
AnswerRe: Getting Wrong Response...help Pin
Rohit Joshi21-Jan-11 9:48
memberRohit Joshi21-Jan-11 9:48 
GeneralRe: Getting Wrong Response...help Pin
jymitra21-Jan-11 20:14
memberjymitra21-Jan-11 20:14 
QuestionMARKING LABELS [modified] Pin
Vaishalimehre13-Oct-10 12:40
memberVaishalimehre13-Oct-10 12:40 
QuestionHelp about store flag "UNSEEN" Pin
jymitra11-Oct-10 21:16
memberjymitra11-Oct-10 21:16 
AnswerRe: Help about store flag "UNSEEN" Pin
Rohit Joshi12-Oct-10 15:34
memberRohit Joshi12-Oct-10 15:34 
GeneralRe: Help about store flag "UNSEEN" Pin
jymitra12-Oct-10 20:21
memberjymitra12-Oct-10 20:21 

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
Web04 | 2.8.150731.1 | Last Updated 20 Sep 2012
Article Copyright 2004 by Rohit Joshi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid