Click here to Skip to main content
Licence MPL
First Posted 16 Aug 2004
Views 485,325
Downloads 6,876
Bookmarked 117 times

IMAP Client library using C#

By | 13 Jan 2005 | Article
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

License

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

About the Author

Rohit Joshi

Software Developer

United States United States

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow I can get all the messages Pinmembercesar817:02 23 May '12  
QuestionHow to add SSL value in this code? PinmemberMember 77232050:17 17 Dec '11  
QuestionHI Rohit can i have a few moments of your time re moving emails from folder to sub folder please Pinmembergarryihill4:11 22 Sep '11  
AnswerRe: HI Rohit can i have a few moments of your time re moving emails from folder to sub folder please PinmemberRohit Joshi4:00 4 Apr '12  
QuestionHow do you retreive attachments? PinmemberDan Guzman12:36 14 Sep '11  
AnswerRe: How do you retreive attachments? PinmemberRohit Joshi4:00 4 Apr '12  
GeneralCannot connect to Gmail IMAP on Port 993 PinmemberKmistry11:09 16 Jun '11  
GeneralRe: Cannot connect to Gmail IMAP on Port 993 PinmemberSyed Majid Ali Gardezi1:35 10 Oct '11  
QuestionGetting Email size PinmemberRamaNeni0:17 13 May '11  
GeneralError: Failure connecting to the IMAP server. imap.gmail.com Pinmembernyeinchan.ucsm17:26 4 May '11  
GeneralRe: Error: Failure connecting to the IMAP server. imap.gmail.com PinmemberAnushaRose0:08 12 Apr '12  
GeneralCommand "UID SEARCH FROM" in Exchange 2010 PinmemberMember 76995525:21 25 Feb '11  
GeneralDid SSL ever get added PinmemberScott W Bowyer12:27 24 Feb '11  
GeneralNewbie Q, i dont understand something PinmemberGothrek1:12 21 Feb '11  
GeneralRe: Newbie Q, i dont understand something PinmemberRohit Joshi4:02 4 Apr '12  
QuestionGetting Wrong Response...help Pinmemberjymitra23:32 28 Dec '10  
AnswerRe: Getting Wrong Response...help PinmemberRohit Joshi9:48 21 Jan '11  
GeneralRe: Getting Wrong Response...help Pinmemberjymitra20:14 21 Jan '11  
QuestionMARKING LABELS [modified] PinmemberVaishalimehre12:40 13 Oct '10  
QuestionHelp about store flag "UNSEEN" Pinmemberjymitra21:16 11 Oct '10  
AnswerRe: Help about store flag "UNSEEN" PinmemberRohit Joshi15:34 12 Oct '10  
GeneralRe: Help about store flag "UNSEEN" Pinmemberjymitra20:21 12 Oct '10  
GeneralReceiving LONG Messags-Bodies PinmemberCFloor22:50 5 Oct '10  
GeneralRe: Receiving LONG Messags-Bodies PinmemberRohit Joshi15:32 12 Oct '10  
GeneralSSL Support Pinmemberdorphalsig8:19 19 Aug '10  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 14 Jan 2005
Article Copyright 2004 by Rohit Joshi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid