Click here to Skip to main content
13,252,537 members (50,917 online)
Click here to Skip to main content
Add your own
alternative version

Stats

953.1K views
27.9K downloads
183 bookmarked
Posted 16 Aug 2004
MPL

IMAP Client library using C#

, 20 Sep 2012
Rate this:
Please Sign up or sign in to vote.
IMAPLibrary supports the basic IMAP protocol functions to fetch messages from the mailbox.

GitHub Link: https://github.com/rohitjoshi/ImapLibrary

 

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 and PR welcome at github repository!!

 

 

 

 

License

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

Share

About the Author

Rohit Joshi
Software Developer
United States United States
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.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralRe: STORE command failure Pin
Michael Mak17-Oct-12 13:00
memberMichael Mak17-Oct-12 13:00 
QuestionFailure bodystructure command Pin
Member 800245416-Oct-12 6:44
memberMember 800245416-Oct-12 6:44 
AnswerRe: Failure bodystructure command Pin
Vibin Valsalan16-Sep-15 10:00
memberVibin Valsalan16-Sep-15 10:00 
QuestionFailure fetching message from IMAP folder/mailbox. BODY[HEADER] Pin
Member 800245416-Oct-12 6:30
memberMember 800245416-Oct-12 6:30 
QuestionFailure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Member 800245416-Oct-12 6:24
memberMember 800245416-Oct-12 6:24 
AnswerRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
PooranPrasad30-May-13 23:07
memberPooranPrasad30-May-13 23:07 
GeneralRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Javier Suero3-Jul-13 3:43
memberJavier Suero3-Jul-13 3:43 
GeneralRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Member 800245418-Mar-14 1:26
memberMember 800245418-Mar-14 1:26 
AnswerRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Rohit Joshi8-Jun-13 16:51
memberRohit Joshi8-Jun-13 16:51 
GeneralRe: Failure searching IMAP with the given criteria. IMAP004 BAD Could not parse command Pin
Javier Suero3-Jul-13 7:35
memberJavier Suero3-Jul-13 7:35 
QuestionCan't obtain email body Pin
eladfrn16-Oct-12 5:46
membereladfrn16-Oct-12 5:46 
GeneralMessage Closed Pin
8-Oct-12 3:19
memberrk.tedlapu8-Oct-12 3:19 
GeneralRe: My vote of 4 Pin
Rohit Joshi8-Oct-12 4:44
memberRohit Joshi8-Oct-12 4:44 
QuestionHow to save the mail in "sent items" folder Pin
rk.tedlapu8-Oct-12 3:14
memberrk.tedlapu8-Oct-12 3:14 
Hi Rohit,

Its really nice script and its very helpful.

I am looking for saving the mail in "sent items" in exchange server using IMAP. could you please help me on this?



Thanks in advance,
Ravi
AnswerRe: How to save the mail in "sent items" folder Pin
Rohit Joshi8-Oct-12 4:43
memberRohit Joshi8-Oct-12 4:43 
GeneralRe: How to save the mail in "sent items" folder Pin
rk.tedlapu8-Oct-12 5:00
memberrk.tedlapu8-Oct-12 5:00 
Questionhelp me using IMAP Client library Pin
supperthin022-Oct-12 22:38
membersupperthin022-Oct-12 22:38 
AnswerRe: help me using IMAP Client library Pin
Rohit Joshi8-Oct-12 4:33
memberRohit Joshi8-Oct-12 4:33 
QuestionCannot read email send using any smartphone (blackberry,iphone, android) Pin
eveniza23-Sep-12 18:47
membereveniza23-Sep-12 18:47 
AnswerRe: Cannot read email send using any smartphone (blackberry,iphone, android) Pin
Rohit Joshi8-Oct-12 4:29
memberRohit Joshi8-Oct-12 4:29 
Questionhow to search gmail spam folder Pin
HoeKeat19-Sep-12 18:53
memberHoeKeat19-Sep-12 18:53 
AnswerRe: how to search gmail spam folder Pin
Rohit Joshi20-Sep-12 6:21
memberRohit Joshi20-Sep-12 6:21 
GeneralRe: how to search gmail spam folder Pin
Brian High29-Sep-13 4:17
memberBrian High29-Sep-13 4:17 
Questionhow to mark email to UNREAD Pin
eveniza13-Aug-12 17:19
membereveniza13-Aug-12 17:19 
AnswerRe: how to mark email to UNREAD Pin
Roysten181819-Sep-12 2:19
memberRoysten181819-Sep-12 2:19 
AnswerRe: how to mark email to UNREAD Pin
Rohit Joshi19-Sep-12 7:44
memberRohit Joshi19-Sep-12 7:44 
GeneralHow to get plan text mail Pin
sajiunni26-Jun-12 3:59
membersajiunni26-Jun-12 3:59 
GeneralRe: How to get plan text mail Pin
Rohit Joshi26-Jun-12 12:29
memberRohit Joshi26-Jun-12 12:29 
GeneralRe: How to get plan text mail Pin
sajiunni26-Jun-12 22:54
membersajiunni26-Jun-12 22:54 
QuestionIndexOutOfRangeException problem Pin
Aces11715-Jun-12 10:55
memberAces11715-Jun-12 10:55 
QuestionHow I can get all the messages Pin
cesar823-May-12 18:02
membercesar823-May-12 18:02 
AnswerRe: How I can get all the messages Pin
Rohit Joshi6-Jun-12 10:04
memberRohit Joshi6-Jun-12 10:04 
GeneralRe: How I can get all the messages Pin
anheru15-Jun-12 4:30
memberanheru15-Jun-12 4:30 
GeneralRe: How I can get all the messages Pin
coder77127-Jan-16 23:35
professionalcoder77127-Jan-16 23:35 
QuestionHow to add SSL value in this code? Pin
Member 772320517-Dec-11 1:17
memberMember 772320517-Dec-11 1:17 
AnswerRe: How to add SSL value in this code? Pin
Rohit Joshi6-Jun-12 11:36
memberRohit Joshi6-Jun-12 11:36 
QuestionHI Rohit can i have a few moments of your time re moving emails from folder to sub folder please Pin
garryihill22-Sep-11 5:11
membergarryihill22-Sep-11 5:11 
AnswerRe: HI Rohit can i have a few moments of your time re moving emails from folder to sub folder please Pin
Rohit Joshi4-Apr-12 5:00
memberRohit Joshi4-Apr-12 5:00 
AnswerRe: HI Rohit can i have a few moments of your time re moving emails from folder to sub folder please Pin
Rohit Joshi6-Jun-12 11:09
memberRohit Joshi6-Jun-12 11:09 
QuestionHow do you retreive attachments? Pin
Dan Guzman14-Sep-11 13:36
memberDan Guzman14-Sep-11 13:36 
AnswerRe: How do you retreive attachments? Pin
Rohit Joshi4-Apr-12 5:00
memberRohit Joshi4-Apr-12 5:00 
GeneralRe: How do you retreive attachments? Pin
vishal.suthar21-Jul-12 4:01
membervishal.suthar21-Jul-12 4:01 
GeneralCannot connect to Gmail IMAP on Port 993 Pin
Kmistry16-Jun-11 12:09
memberKmistry16-Jun-11 12:09 
GeneralRe: Cannot connect to Gmail IMAP on Port 993 Pin
Syed Majid Ali Gardezi10-Oct-11 2:35
memberSyed Majid Ali Gardezi10-Oct-11 2:35 
QuestionGetting Email size Pin
RamaNeni13-May-11 1:17
memberRamaNeni13-May-11 1:17 
AnswerRe: Getting Email size Pin
Rohit Joshi7-Jun-12 8:54
memberRohit Joshi7-Jun-12 8:54 
GeneralError: Failure connecting to the IMAP server. imap.gmail.com Pin
nyeinchan.ucsm4-May-11 18:26
membernyeinchan.ucsm4-May-11 18:26 
GeneralRe: Error: Failure connecting to the IMAP server. imap.gmail.com Pin
AnushaRose12-Apr-12 1:08
memberAnushaRose12-Apr-12 1:08 
GeneralRe: Error: Failure connecting to the IMAP server. imap.gmail.com Pin
Rohit Joshi6-Jun-12 11:09
memberRohit Joshi6-Jun-12 11:09 
GeneralCommand "UID SEARCH FROM" in Exchange 2010 Pin
Member 769955225-Feb-11 6:21
memberMember 769955225-Feb-11 6:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 20 Sep 2012
Article Copyright 2004 by Rohit Joshi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid