Click here to Skip to main content
12,401,189 members (61,515 online)
Click here to Skip to main content
Add your own
alternative version

Stats

812.8K views
25.8K downloads
182 bookmarked
Posted

IMAP Client library using C#

, 20 Sep 2012 MPL
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...

Comments and Discussions

 
QuestionCongratulations. How to get todays emails? Pin
Member 118840547-Mar-16 0:07
memberMember 118840547-Mar-16 0:07 
Questionhow to use it? I'm beginner Pin
coder77127-Jan-16 2:10
professionalcoder77127-Jan-16 2:10 
AnswerRe: how to use it? I'm beginner Pin
Garth J Lancaster27-Jan-16 2:43
professionalGarth J Lancaster27-Jan-16 2:43 
GeneralRe: how to use it? I'm beginner Pin
coder77127-Jan-16 4:43
professionalcoder77127-Jan-16 4:43 
QuestionError while connecting to server Pin
Navin Pandit16-Jan-16 22:08
memberNavin Pandit16-Jan-16 22:08 
AnswerRe: Error while connecting to server Pin
AJ_Smit28-Mar-16 22:09
memberAJ_Smit28-Mar-16 22:09 
Questionmore info on question below Pin
Member 1201959512-Oct-15 0:26
memberMember 1201959512-Oct-15 0:26 
Questionemail with attachement but no body Pin
Member 120195959-Oct-15 1:54
memberMember 120195959-Oct-15 1:54 
QuestionSaving Attachment from the XML file Pin
Vibin Valsalan16-Sep-15 9:16
memberVibin Valsalan16-Sep-15 9:16 
QuestionHow could I get the attachment? Pin
Dolby.Xiaowen24-Jul-15 15:15
memberDolby.Xiaowen24-Jul-15 15:15 
AnswerRe: How could I get the attachment? Pin
Rohit Joshi4-Sep-15 4:47
memberRohit Joshi4-Sep-15 4:47 
QuestionRe: How could I get the attachment? Pin
Member 121955619-Dec-15 1:44
memberMember 121955619-Dec-15 1:44 
QuestionIs it possible to use the source code in commercial applications Pin
Member 116659175-May-15 3:15
memberMember 116659175-May-15 3:15 
AnswerRe: Is it possible to use the source code in commercial applications Pin
Rohit Joshi4-Sep-15 4:35
memberRohit Joshi4-Sep-15 4:35 
BugBug with large attachments Pin
Alexey T9-Mar-15 20:31
memberAlexey T9-Mar-15 20:31 
GeneralRe: Bug with large attachments Pin
Rohit Joshi4-Sep-15 4:46
memberRohit Joshi4-Sep-15 4:46 
GeneralRe: Bug with large attachments Pin
Galiu Manuel9-Dec-15 2:28
memberGaliu Manuel9-Dec-15 2:28 
QuestionAuthenication Failed Error Pin
Santhakumari S26-Feb-15 0:33
memberSanthakumari S26-Feb-15 0:33 
AnswerRe: Authenication Failed Error Pin
Rohit Joshi4-Sep-15 4:44
memberRohit Joshi4-Sep-15 4:44 
QuestionNew E-Mail Check. Pin
Member 1116351120-Oct-14 11:07
memberMember 1116351120-Oct-14 11:07 
QuestionCrashing at logout with polish mail serivce Pin
Darek2229-Sep-14 1:35
memberDarek2229-Sep-14 1:35 
NewsNuGet Package Pin
CiganoMorrison9-Mar-14 20:21
professionalCiganoMorrison9-Mar-14 20:21 
GeneralRe: NuGet Package Pin
Rohit Joshi4-Sep-15 4:43
memberRohit Joshi4-Sep-15 4:43 
GeneralMy vote of 5 Pin
thaiphong.pham15-Dec-13 22:12
memberthaiphong.pham15-Dec-13 22:12 
GeneralRe: My vote of 5 Pin
Rohit Joshi4-Sep-15 4:36
memberRohit Joshi4-Sep-15 4:36 
QuestionIntegration of gmail mails into asp.net using IMAP protocol Pin
surbhinewaskar20-Oct-13 23:01
membersurbhinewaskar20-Oct-13 23:01 
AnswerRe: Integration of gmail mails into asp.net using IMAP protocol Pin
Ravi Bhavnani5-Nov-13 9:58
professionalRavi Bhavnani5-Nov-13 9:58 
AnswerRe: Integration of gmail mails into asp.net using IMAP protocol Pin
Rohit Joshi4-Sep-15 4:42
memberRohit Joshi4-Sep-15 4:42 
Questioncannot fetch body message on yahoomail Pin
rondrac7829-Jul-13 14:04
memberrondrac7829-Jul-13 14:04 
AnswerRe: cannot fetch body message on yahoomail Pin
Ravi Bhavnani5-Nov-13 9:58
professionalRavi Bhavnani5-Nov-13 9:58 
AnswerRe: cannot fetch body message on yahoomail Pin
Rohit Joshi4-Sep-15 4:40
memberRohit Joshi4-Sep-15 4:40 
Questionhow to get all the emails in the folder? Pin
sista4-Jul-13 22:07
membersista4-Jul-13 22:07 
AnswerRe: how to get all the emails in the folder? Pin
Rohit Joshi4-Sep-15 4:38
memberRohit Joshi4-Sep-15 4:38 
GeneralMy vote of 5 Pin
Impenneteri22-Mar-13 10:01
memberImpenneteri22-Mar-13 10:01 
GeneralRe: My vote of 5 Pin
Rohit Joshi8-Jun-13 15:55
memberRohit Joshi8-Jun-13 15:55 
QuestionUnable to find the Message Body while fetching the mail Pin
ramya.km29-Jan-13 22:03
memberramya.km29-Jan-13 22:03 
AnswerRe: Unable to find the Message Body while fetching the mail Pin
Rohit Joshi8-Jun-13 15:55
memberRohit Joshi8-Jun-13 15:55 
QuestionProblem with attachment fileName Pin
Member 96878062-Jan-13 2:59
memberMember 96878062-Jan-13 2:59 
AnswerRe: Problem with attachment fileName Pin
Rohit Joshi24-Jan-13 4:18
memberRohit Joshi24-Jan-13 4:18 
QuestionMissing parameter in Restore Func Pin
Ikarus7631-Dec-12 0:28
memberIkarus7631-Dec-12 0:28 
AnswerRe: Missing parameter in Restore Func Pin
Rohit Joshi24-Jan-13 4:15
memberRohit Joshi24-Jan-13 4:15 
QuestionGMail problem, only for GoogleApp account Pin
roberto.roncato11-Dec-12 21:24
memberroberto.roncato11-Dec-12 21:24 
AnswerRe: GMail problem, only for GoogleApp account Pin
roberto.roncato11-Dec-12 21:38
memberroberto.roncato11-Dec-12 21:38 
GeneralRe: GMail problem, only for GoogleApp account Pin
Rohit Joshi24-Jan-13 4:15
memberRohit Joshi24-Jan-13 4:15 
QuestionSearching email subject vlues Pin
thetra30-Nov-12 3:43
memberthetra30-Nov-12 3:43 
AnswerRe: Searching email subject vlues Pin
Rohit Joshi30-Nov-12 6:30
memberRohit Joshi30-Nov-12 6:30 
GeneralRe: Searching email subject vlues Pin
thetra5-Dec-12 22:20
memberthetra5-Dec-12 22:20 
GeneralRe: Searching email subject vlues Pin
Rohit Joshi24-Jan-13 4:17
memberRohit Joshi24-Jan-13 4:17 
QuestionHow to use SearchMessage Pin
Christian A Berczely26-Oct-12 7:27
memberChristian A Berczely26-Oct-12 7:27 
AnswerRe: How to use SearchMessage Pin
Rohit Joshi29-Oct-12 10:08
memberRohit Joshi29-Oct-12 10:08 

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.

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