Click here to Skip to main content
Click here to Skip to main content

IMAP Client library using C#

By , 20 Sep 2012
 

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)

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberImpenneteri22 Mar '13 - 10:01 
QuestionUnable to find the Message Body while fetching the mailmemberramya.km29 Jan '13 - 22:03 
QuestionProblem with attachment fileNamememberMember 96878062 Jan '13 - 2:59 
AnswerRe: Problem with attachment fileNamememberRohit Joshi24 Jan '13 - 4:18 
QuestionMissing parameter in Restore FuncmemberIkarus7631 Dec '12 - 0:28 
Hi,
the re-login in Restore(bool) is done without the ssl flag. I assume the line should look like:
 
Login(m_sHost, m_nPort, m_sUserId, m_sPassword, m_bSSLEnabled);
 
regards,
Michael
AnswerRe: Missing parameter in Restore FuncmemberRohit Joshi24 Jan '13 - 4:15 
QuestionGMail problem, only for GoogleApp accountmemberroberto.roncato11 Dec '12 - 21:24 
AnswerRe: GMail problem, only for GoogleApp accountmemberroberto.roncato11 Dec '12 - 21:38 
GeneralRe: GMail problem, only for GoogleApp accountmemberRohit Joshi24 Jan '13 - 4:15 
QuestionSearching email subject vluesmemberthetra30 Nov '12 - 3:43 
AnswerRe: Searching email subject vlues [modified]memberRohit Joshi30 Nov '12 - 6:30 
GeneralRe: Searching email subject vluesmemberthetra5 Dec '12 - 22:20 
GeneralRe: Searching email subject vluesmemberRohit Joshi24 Jan '13 - 4:17 
QuestionHow to use SearchMessagememberChristian A Berczely26 Oct '12 - 7:27 
AnswerRe: How to use SearchMessagememberRohit Joshi29 Oct '12 - 10:08 
QuestionAdd mail to sent itemsmemberRenoMarseille19 Oct '12 - 23:40 
QuestionSTORE command failurememberMichael Mak16 Oct '12 - 18:10 
AnswerRe: STORE command failurememberRohit Joshi17 Oct '12 - 4:03 
GeneralRe: STORE command failurememberMichael Mak17 Oct '12 - 12:00 
QuestionFailure bodystructure commandmemberMember 800245416 Oct '12 - 5:44 
QuestionFailure fetching message from IMAP folder/mailbox. BODY[HEADER]memberMember 800245416 Oct '12 - 5:30 
QuestionFailure searching IMAP with the given criteria. IMAP004 BAD Could not parse commandmemberMember 800245416 Oct '12 - 5:24 
QuestionCan't obtain email bodymembereladfrn16 Oct '12 - 4:46 
GeneralMessage Removedmemberrk.tedlapu8 Oct '12 - 2:19 
GeneralRe: My vote of 4memberRohit Joshi8 Oct '12 - 3:44 
QuestionHow to save the mail in "sent items" foldermemberrk.tedlapu8 Oct '12 - 2:14 
AnswerRe: How to save the mail in "sent items" foldermemberRohit Joshi8 Oct '12 - 3:43 
GeneralRe: How to save the mail in "sent items" foldermemberrk.tedlapu8 Oct '12 - 4:00 
Questionhelp me using IMAP Client librarymembersupperthin022 Oct '12 - 21:38 
AnswerRe: help me using IMAP Client librarymemberRohit Joshi8 Oct '12 - 3:33 
QuestionCannot read email send using any smartphone (blackberry,iphone, android)membereveniza23 Sep '12 - 17:47 
AnswerRe: Cannot read email send using any smartphone (blackberry,iphone, android)memberRohit Joshi8 Oct '12 - 3:29 
Questionhow to search gmail spam foldermemberHoeKeat19 Sep '12 - 17:53 
AnswerRe: how to search gmail spam foldermemberRohit Joshi20 Sep '12 - 5:21 
Questionhow to mark email to UNREADmembereveniza13 Aug '12 - 16:19 
AnswerRe: how to mark email to UNREADmemberRoysten181819 Sep '12 - 1:19 
AnswerRe: how to mark email to UNREADmemberRohit Joshi19 Sep '12 - 6:44 
GeneralHow to get plan text mailmembersajiunni26 Jun '12 - 2:59 
GeneralRe: How to get plan text mailmemberRohit Joshi26 Jun '12 - 11:29 
GeneralRe: How to get plan text mailmembersajiunni26 Jun '12 - 21:54 
QuestionIndexOutOfRangeException problemmemberAces11715 Jun '12 - 9:55 
QuestionHow I can get all the messagesmembercesar823 May '12 - 17:02 
AnswerRe: How I can get all the messagesmemberRohit Joshi6 Jun '12 - 9:04 
GeneralRe: How I can get all the messagesmemberanheru15 Jun '12 - 3:30 
QuestionHow to add SSL value in this code?memberMember 772320517 Dec '11 - 0:17 
AnswerRe: How to add SSL value in this code?memberRohit Joshi6 Jun '12 - 10:36 
QuestionHI Rohit can i have a few moments of your time re moving emails from folder to sub folder pleasemembergarryihill22 Sep '11 - 4:11 
AnswerRe: HI Rohit can i have a few moments of your time re moving emails from folder to sub folder pleasememberRohit Joshi4 Apr '12 - 4:00 
AnswerRe: HI Rohit can i have a few moments of your time re moving emails from folder to sub folder pleasememberRohit Joshi6 Jun '12 - 10:09 
QuestionHow do you retreive attachments?memberDan Guzman14 Sep '11 - 12:36 

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

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