Click here to Skip to main content
Click here to Skip to main content

IMAP Client library using C#

By , 20 Sep 2012
 

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 !!

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberImpenneteri22 Mar '13 - 10:01 
I'll really learn from this. Kudos man!
QuestionUnable to find the Message Body while fetching the mailmemberramya.km29 Jan '13 - 22:03 
Hi Rohit,
 
Awesome article,
 
I am unable to find the Message body while fetching the email based on the message UID.
Kindly do let me know whether message body is in encrypted format or it is not fetching the same.
 
Message UID[]:174
Fetch Body:[false]true
IMAP004 UID FETCH 174 BODY[HEADER]
* 109 FETCH (BODY[HEADER] {1264}
MIME-Version: 1.0
Received: from SIXPRD0211HT002.apcprd02.prod.outlook.com (10.255.38.165) by
HKXPRD0210HT002.apcprd02.prod.outlook.com (10.255.5.165) with Microsoft SMTP
Server (TLS) id 14.16.257.4; Thu, 17 Jan 2013 12:50:20 +0000
Received: from SIXPRD0211MB443.apcprd02.prod.outlook.com ([169.254.3.223]) by
SIXPRD0211HT002.apcprd02.prod.outlook.com ([10.255.38.165]) with mapi id
14.16.0257.004; Thu, 17 Jan 2013 12:50:20 +0000
From: sdfsdf
To: SDFsdf
CC: sdfsdfsdf
Subject: Regarding Call
Thread-Topic: Regarding Call
Thread-Index: Ac30sTTbHCu6LpNPTMqblRRGClmd/Q==
Date: Thu, 17 Jan 2013 12:50:19 +0000
Message-ID:

Accept-Language: en-US
Content-Language: en-US
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 04
X-MS-Exchange-Organization-AuthSource:
SIXPRD0211HT002.apcprd02.prod.outlook.com
X-MS-Has-Attach:
X-MS-Exchange-Organization-SCL: -1
X-MS-TNEF-Correlator:
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
 
UID 174 FLAGS (\Seen))
IMAP004 OK FETCH completed.
 
IMAP005 UID FETCH 174 BODYSTRUCTURE
* 109 FETCH (BODYSTRUCTURE ("text" "html" ("charset" "iso-8859-1") NIL NIL "quot
ed-printable" 3342 79 NIL NIL "en-US" NIL) UID 174)
IMAP005 OK FETCH completed.
QuestionProblem with attachment fileNamememberMember 96878062 Jan '13 - 2:59 
Hello Rohit
Is InterIMAP Library project always in course ?
It's work well but for most of the mail, attachment file name is not the real one but things like 0FB6C3935BF49BEDB41EA27EBE751@eurprd06.prod.outlook.com.
Did you do correction around that ?
Best regards
AnswerRe: Problem with attachment fileNamememberRohit Joshi24 Jan '13 - 4:18 
No, someone fork from this and created InterImap but don't know the state.
If you have any issues, do let me know and I can provide the fix.
QuestionMissing parameter in Restore FuncmemberIkarus7631 Dec '12 - 0:28 
Hi,
the re-login in Restore(bool) is done without the ssl flag. I assume the line should look like:
 
Login(m_sHost, m_nPort, m_sUserId, m_sPassword, m_bSSLEnabled);
 
regards,
Michael
AnswerRe: Missing parameter in Restore FuncmemberRohit Joshi24 Jan '13 - 4:15 
Thanks. I will fix it and upload new version.
QuestionGMail problem, only for GoogleApp accountmemberroberto.roncato11 Dec '12 - 21:24 
Hi, your library goes very well for my account xxxxxx@gmail.com, but if I try with yyyyy@domain.com, hosted by GMail, this doesn't work.
 
If I use the same params in Thunderbird (imap.gmail.com, SSL, port 993) it works
 
Obviusly I have activated the IMAP access on GMail configuration panel.
 
Someone can help me?
Thanks!
AnswerRe: GMail problem, only for GoogleApp accountmemberroberto.roncato11 Dec '12 - 21:38 
I resolve...
The problem was the password, something like: (Aaaaa0000)
 
I change the password, and all is fine...
GeneralRe: GMail problem, only for GoogleApp accountmemberRohit Joshi24 Jan '13 - 4:15 
Great!!
QuestionSearching email subject vluesmemberthetra30 Nov '12 - 3:43 
Hi! I went through Joshi.Utils.Imap.Imap classes...You have done good work. I'm new to this kind of programming and I wondered if it is possible to get the subjectName and mailDate somewhere in those methods. I want to search through emails particularly on subjectfield and email sent date.

Thanking you

With Regards
Sofi

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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