Click here to Skip to main content
Licence MPL
First Posted 16 Aug 2004
Views 458,559
Downloads 5,015
Bookmarked 113 times

IMAP Client library using C#

By Rohit Joshi | 13 Jan 2005
IMAPLibrary supports the basic IMAP protocol functions to fetch messages from the mailbox.
2 votes, 4.5%
1

2
2 votes, 4.5%
3
13 votes, 29.5%
4
27 votes, 61.4%
5
4.37/5 - 45 votes
2 removed
μ 4.30, σa 1.67 [?]

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 to add SSL value in this code? PinmemberMember 77232051:17 17 Dec '11  
QuestionHI Rohit can i have a few moments of your time re moving emails from folder to sub folder please Pinmembergarryihill5:11 22 Sep '11  
QuestionHow do you retreive attachments? PinmemberDan Guzman13:36 14 Sep '11  
GeneralCannot connect to Gmail IMAP on Port 993 PinmemberKmistry12:09 16 Jun '11  
GeneralRe: Cannot connect to Gmail IMAP on Port 993 PinmemberSyed Majid Ali Gardezi2:35 10 Oct '11  
QuestionGetting Email size PinmemberRamaNeni1:17 13 May '11  
GeneralError: Failure connecting to the IMAP server. imap.gmail.com Pinmembernyeinchan.ucsm18:26 4 May '11  
GeneralCommand "UID SEARCH FROM" in Exchange 2010 PinmemberMember 76995526:21 25 Feb '11  
GeneralDid SSL ever get added PinmemberScott W Bowyer13:27 24 Feb '11  
GeneralNewbie Q, i dont understand something PinmemberGothrek2:12 21 Feb '11  
QuestionGetting Wrong Response...help Pinmemberjymitra0:32 29 Dec '10  
AnswerRe: Getting Wrong Response...help PinmemberRohit Joshi10:48 21 Jan '11  
GeneralRe: Getting Wrong Response...help Pinmemberjymitra21:14 21 Jan '11  
QuestionMARKING LABELS [modified] PinmemberVaishalimehre13:40 13 Oct '10  
QuestionHelp about store flag "UNSEEN" Pinmemberjymitra22:16 11 Oct '10  
AnswerRe: Help about store flag "UNSEEN" PinmemberRohit Joshi16:34 12 Oct '10  
GeneralRe: Help about store flag "UNSEEN" Pinmemberjymitra21:21 12 Oct '10  
GeneralReceiving LONG Messags-Bodies PinmemberCFloor23:50 5 Oct '10  
GeneralRe: Receiving LONG Messags-Bodies PinmemberRohit Joshi16:32 12 Oct '10  
GeneralSSL Support Pinmemberdorphalsig9:19 19 Aug '10  
GeneralRe: SSL Support PinmemberRohit Joshi16:31 12 Oct '10  
GeneralRe: SSL Support Pinmembermohamed antar11:24 20 Nov '10  
GeneralThanks a lot PinmemberKrasshirsch2:40 7 Aug '10  
Questionsomebody wake up Please help about my problems Pinmemberjymitra2:06 30 Jul '10  
AnswerRe: somebody wake up Please help about my problems PinmemberRohit Joshi16:30 12 Oct '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.120206.1 | Last Updated 14 Jan 2005
Article Copyright 2004 by Rohit Joshi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid