Click here to Skip to main content
6,305,776 members and growing! (17,281 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Email     Intermediate License: The Mozilla Public License 1.1 (MPL 1.1)

IMAP Client library using C#

By Rohit Joshi

IMAPLibrary supports the basic IMAP protocol functions to fetch messages from the mailbox.
C#, VC7.NET 1.0, .NET 1.1, WinXPVS.NET2003, Dev
Posted:16 Aug 2004
Updated:13 Jan 2005
Views:165,580
Bookmarked:72 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
30 votes for this article.
Popularity: 6.37 Rating: 4.31 out of 5

1

2
2 votes, 6.9%
3
9 votes, 31.0%
4
18 votes, 62.1%
5

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


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.
Occupation: Software Developer
Location: United States United States

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 196 (Total in Forum: 196) (Refresh)FirstPrevNext
GeneralMark read in SELECT and EXAMINE Pinmemberscorpiobra14:58 8 Jul '09  
GeneralRe: Mark read in SELECT and EXAMINE Pinmemberscorpiobra7hrs 44mins ago 
GeneralRe: Mark read in SELECT and EXAMINE PinmemberRohit Joshi2hrs 58mins ago 
QuestionNot connected to gmail Pinmemberashishshar0:12 12 May '09  
GeneralWhat is syntax for expunge uid? Pinmembermanchu737:45 11 May '09  
GeneralRe: What is syntax for expunge uid? PinmemberRohit Joshi8:48 11 May '09  
GeneralRe: What is syntax for expunge uid? Pinmembermanchu738:52 11 May '09  
GeneralHow to Use this library PinmemberCMC1235:47 15 Apr '09  
GeneralRe: How to Use this library Pinmembernewbish17:32 16 Apr '09  
GeneralWhat is the UID for this Message that I can call Fetch with? PinmemberFrancis Lau15:43 9 Apr '09  
GeneralRe: What is the UID for this Message that I can call Fetch with? PinmemberRohit Joshi8:50 11 May '09  
QuestionProblems downloading messages with cyrus imap 2.2.13-14+b3 Pinmemberpcalder0n17:35 18 Mar '09  
AnswerRe: Problems downloading messages with cyrus imap 2.2.13-14+b3 PinmemberRohit Joshi6:40 24 Mar '09  
GeneralUnable to read data from the transport connection: An existing connection. error code 10054 Pinmemberdeewaker pandey4:18 5 Jan '09  
GeneralRe: Unable to read data from the transport connection: An existing connection. error code 10054 Pinmembercaubai1076:35 8 Jan '09  
Generalhomtail or yahoo Pinmemberjoekey7:53 28 Dec '08  
GeneralRe: homtail or yahoo PinmemberRohit Joshi8:54 11 May '09  
QuestionBug in ParseBodyStructure (???????????) Pinmemberaragorn1234567895:38 19 Sep '08  
AnswerRe: Bug in ParseBodyStructure (???????????) PinmemberAnjanaDurga19:50 19 Sep '08  
GeneralRe: Bug in ParseBodyStructure (???????????) Pinmemberaragorn1234567891:48 20 Sep '08  
GeneralRe: Bug in ParseBodyStructure (???????????) Pinmemberaragorn1234567892:01 20 Sep '08  
GeneralRe: Bug in ParseBodyStructure (???????????) PinmemberAnjanaDurga20:25 22 Sep '08  
GeneralRe: Bug in ParseBodyStructure (???????????) Pinmemberaragorn12345678922:06 22 Sep '08  
QuestionRegarding IMAP FetchMessage Method [modified] PinmemberAnjanaDurga3:35 13 Sep '08  
AnswerRe: Regarding IMAP FetchMessage Method PinmemberRohit Joshi8:52 11 May '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Jan 2005
Editor: Genevieve Sovereign
Copyright 2004 by Rohit Joshi
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project