5,448,416 members and growing! (18,867 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, C++Windows, .NET, .NET 1.0, .NET 1.1, WinXPVS.NET2002, VS.NET2003, Visual Studio, Dev

Posted: 16 Aug 2004
Updated: 13 Jan 2005
Views: 112,086
Bookmarked: 57 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
26 votes for this Article.
Popularity: 6.00 Rating: 4.24 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
2 votes, 8.0%
3
9 votes, 36.0%
4
14 votes, 56.0%
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


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: Web Developer
Location: United States United States

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 166 (Total in Forum: 166) (Refresh)FirstPrevNext
Subject  Author Date 
Questionm_nTotalMessagesmemberCezW12:58 26 Aug '08  
AnswerRe: m_nTotalMessagesmemberRohit Joshi14:18 5 Sep '08  
QuestionHow to import contacts from an IMAP server in C# [modified]memberBobb198220:23 1 Jul '08  
AnswerRe: How to import contacts from an IMAP server in C#memberRohit Joshi5:45 2 Jul '08  
GeneralRe: How to import contacts from an IMAP server in C#memberBobb198223:36 3 Jul '08  
GeneralReading email using IMAP in asp.netmemberNirad Smart3:08 20 May '08  
GeneralRe: Reading email using IMAP in asp.netmemberAtmospherian8:13 20 May '08  
QuestionWant to get Attachments of a message which is in Byte[]. I am using IMAPmemberIrfan Shahid2:46 12 May '08  
AnswerRe: Want to get Attachments of a message which is in Byte[]. I am using IMAP [modified]memberAtmospherian8:06 20 May '08  
Questionbody-content with-out XMLmemberrsharda15:35 11 May '08  
AnswerRe: body-content with-out XMLmemberRohit Joshi5:05 12 May '08  
AnswerRe: body-content with-out XML [modified]memberAtmospherian7:59 20 May '08  
GeneralFetching an entire message bodymemberMember 215276712:39 29 Apr '08  
GeneralRe: Fetching an entire message body [modified]memberAtmospherian7:57 20 May '08  
GeneralGmail Using the new SSL CodememberCanadaDan13:57 28 Apr '08  
AnswerRe: Gmail Using the new SSL Codememberferdna9:25 29 Apr '08  
GeneralRe: Gmail Using the new SSL Code [modified]memberAtmospherian7:57 20 May '08  
Generalnotepad attachementmemberjoekey23:59 14 Apr '08  
GeneralRe: notepad attachementmemberRohit Joshi4:26 15 Apr '08  
GeneralRe: notepad attachementmemberjoekey20:48 15 Apr '08  
GeneralRe: notepad attachementmemberjoekey1:45 18 Apr '08  
GeneralRe: notepad attachementmemberslymenk1:47 21 Apr '08  
GeneralRe: notepad attachement [modified]memberRohit Joshi14:17 21 Apr '08  
GeneralRe: notepad attachementmemberjoekey1:05 6 May '08  
GeneralMove a message into a folder?membertbrewer12:01 11 Apr '08  

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-2008
Web20 | Advertise on the Code Project