Click here to Skip to main content
15,894,106 members
Articles / Web Development / HTML

IMAP Client library using C#

Rate me:
Please Sign up or sign in to vote.
4.65/5 (88 votes)
20 Sep 2012MPL2 min read 1.4M   31.9K   184  
IMAPLibrary supports the basic IMAP protocol functions to fetch messages from the mailbox.
  • ImapLibrary.zip
    • ImapLibrary
      • Documentation
        • banner.htm
        • banner.jpg
        • CodeCommentReportError.htm
        • commentreport.css
        • darkcorner.jpg
        • gradleft.jpg
        • gradtop.jpg
        • graycorner.jpg
        • ImapLibrary
          • CWP0.HTM
          • CWP10.HTM
          • CWP100.HTM
          • CWP101.HTM
          • CWP102.HTM
          • CWP103.HTM
          • CWP104.HTM
          • CWP105.HTM
          • CWP106.HTM
          • CWP107.HTM
          • CWP108.HTM
          • CWP109.HTM
          • CWP11.HTM
          • CWP110.HTM
          • CWP111.HTM
          • CWP112.HTM
          • CWP113.HTM
          • CWP114.HTM
          • CWP115.HTM
          • CWP116.HTM
          • CWP117.HTM
          • CWP118.HTM
          • CWP119.HTM
          • CWP12.HTM
          • CWP120.HTM
          • CWP121.HTM
          • CWP122.HTM
          • CWP123.HTM
          • CWP125.HTM
          • CWP126.HTM
          • CWP127.HTM
          • CWP128.HTM
          • CWP13.HTM
          • CWP130.HTM
          • CWP131.HTM
          • CWP132.HTM
          • CWP133.HTM
          • CWP134.HTM
          • CWP135.HTM
          • CWP136.HTM
          • CWP137.HTM
          • CWP138.HTM
          • CWP139.HTM
          • CWP14.HTM
          • CWP140.HTM
          • CWP141.HTM
          • CWP142.HTM
          • CWP143.HTM
          • CWP144.HTM
          • CWP146.HTM
          • CWP147.HTM
          • CWP148.HTM
          • CWP149.HTM
          • CWP15.HTM
          • CWP150.HTM
          • CWP151.HTM
          • CWP152.HTM
          • CWP153.HTM
          • CWP154.HTM
          • CWP155.HTM
          • CWP156.HTM
          • CWP157.HTM
          • CWP158.HTM
          • CWP159.HTM
          • CWP16.HTM
          • CWP160.HTM
          • CWP161.HTM
          • CWP162.HTM
          • CWP163.HTM
          • CWP164.HTM
          • CWP165.HTM
          • CWP166.HTM
          • CWP167.HTM
          • CWP168.HTM
          • CWP169.HTM
          • CWP17.HTM
          • CWP170.HTM
          • CWP171.HTM
          • CWP172.HTM
          • CWP173.HTM
          • CWP174.HTM
          • CWP175.HTM
          • CWP176.HTM
          • CWP177.HTM
          • CWP178.HTM
          • CWP179.HTM
          • CWP18.HTM
          • CWP180.HTM
          • CWP181.HTM
          • CWP182.HTM
          • CWP183.HTM
          • CWP19.HTM
          • CWP2.HTM
          • CWP20.HTM
          • CWP21.HTM
          • CWP22.HTM
          • CWP23.HTM
          • CWP24.HTM
          • CWP25.HTM
          • CWP26.HTM
          • CWP27.HTM
          • CWP28.HTM
          • CWP29.HTM
          • CWP3.HTM
          • CWP30.HTM
          • CWP31.HTM
          • CWP32.HTM
          • CWP33.HTM
          • CWP34.HTM
          • CWP35.HTM
          • CWP36.HTM
          • CWP37.HTM
          • CWP38.HTM
          • CWP39.HTM
          • CWP4.HTM
          • CWP40.HTM
          • CWP41.HTM
          • CWP42.HTM
          • CWP43.HTM
          • CWP44.HTM
          • CWP45.HTM
          • CWP46.HTM
          • CWP47.HTM
          • CWP48.HTM
          • CWP49.HTM
          • CWP5.HTM
          • CWP50.HTM
          • CWP51.HTM
          • CWP52.HTM
          • CWP53.HTM
          • CWP54.HTM
          • CWP55.HTM
          • CWP56.HTM
          • CWP57.HTM
          • CWP58.HTM
          • CWP59.HTM
          • CWP6.HTM
          • CWP60.HTM
          • CWP61.HTM
          • CWP62.HTM
          • CWP63.HTM
          • CWP64.HTM
          • CWP65.HTM
          • CWP66.HTM
          • CWP67.HTM
          • CWP68.HTM
          • CWP69.HTM
          • CWP7.HTM
          • CWP70.HTM
          • CWP71.HTM
          • CWP72.HTM
          • CWP73.HTM
          • CWP74.HTM
          • CWP75.HTM
          • CWP76.HTM
          • CWP77.HTM
          • CWP78.HTM
          • CWP79.HTM
          • CWP8.HTM
          • CWP80.HTM
          • CWP81.HTM
          • CWP82.HTM
          • CWP83.HTM
          • CWP84.HTM
          • CWP85.HTM
          • CWP86.HTM
          • CWP87.HTM
          • CWP88.HTM
          • CWP89.HTM
          • CWP9.HTM
          • CWP90.HTM
          • CWP91.HTM
          • CWP92.HTM
          • CWP93.HTM
          • CWP94.HTM
          • CWP95.HTM
          • CWP96.HTM
          • CWP97.HTM
          • CWP98.HTM
          • CWP99.HTM
          • ImapLibrary.HTM
        • minus.jpg
        • plus.jpg
        • Solution_ImapLibrary.HTM
        • titletile.jpg
        • vt.js
      • ImapLibrary.sln
      • ImapLibrary.sln.DotSettings.user
      • ImapLibrary
      • ImapLibraryTest
    • ImapLibraryTest.png
  • ImapLibrary-noexe.zip
    • banner.htm
    • banner.jpg
    • CodeCommentReportError.htm
    • commentreport.css
    • darkcorner.jpg
    • gradleft.jpg
    • gradtop.jpg
    • graycorner.jpg
    • CWP0.HTM
    • CWP10.HTM
    • CWP100.HTM
    • CWP101.HTM
    • CWP102.HTM
    • CWP103.HTM
    • CWP104.HTM
    • CWP105.HTM
    • CWP106.HTM
    • CWP107.HTM
    • CWP108.HTM
    • CWP109.HTM
    • CWP11.HTM
    • CWP110.HTM
    • CWP111.HTM
    • CWP112.HTM
    • CWP113.HTM
    • CWP114.HTM
    • CWP115.HTM
    • CWP116.HTM
    • CWP117.HTM
    • CWP118.HTM
    • CWP119.HTM
    • CWP12.HTM
    • CWP120.HTM
    • CWP121.HTM
    • CWP122.HTM
    • CWP123.HTM
    • CWP125.HTM
    • CWP126.HTM
    • CWP127.HTM
    • CWP128.HTM
    • CWP13.HTM
    • CWP130.HTM
    • CWP131.HTM
    • CWP132.HTM
    • CWP133.HTM
    • CWP134.HTM
    • CWP135.HTM
    • CWP136.HTM
    • CWP137.HTM
    • CWP138.HTM
    • CWP139.HTM
    • CWP14.HTM
    • CWP140.HTM
    • CWP141.HTM
    • CWP142.HTM
    • CWP143.HTM
    • CWP144.HTM
    • CWP146.HTM
    • CWP147.HTM
    • CWP148.HTM
    • CWP149.HTM
    • CWP15.HTM
    • CWP150.HTM
    • CWP151.HTM
    • CWP152.HTM
    • CWP153.HTM
    • CWP154.HTM
    • CWP155.HTM
    • CWP156.HTM
    • CWP157.HTM
    • CWP158.HTM
    • CWP159.HTM
    • CWP16.HTM
    • CWP160.HTM
    • CWP161.HTM
    • CWP162.HTM
    • CWP163.HTM
    • CWP164.HTM
    • CWP165.HTM
    • CWP166.HTM
    • CWP167.HTM
    • CWP168.HTM
    • CWP169.HTM
    • CWP17.HTM
    • CWP170.HTM
    • CWP171.HTM
    • CWP172.HTM
    • CWP173.HTM
    • CWP174.HTM
    • CWP175.HTM
    • CWP176.HTM
    • CWP177.HTM
    • CWP178.HTM
    • CWP179.HTM
    • CWP18.HTM
    • CWP180.HTM
    • CWP181.HTM
    • CWP182.HTM
    • CWP183.HTM
    • CWP19.HTM
    • CWP2.HTM
    • CWP20.HTM
    • CWP21.HTM
    • CWP22.HTM
    • CWP23.HTM
    • CWP24.HTM
    • CWP25.HTM
    • CWP26.HTM
    • CWP27.HTM
    • CWP28.HTM
    • CWP29.HTM
    • CWP3.HTM
    • CWP30.HTM
    • CWP31.HTM
    • CWP32.HTM
    • CWP33.HTM
    • CWP34.HTM
    • CWP35.HTM
    • CWP36.HTM
    • CWP37.HTM
    • CWP38.HTM
    • CWP39.HTM
    • CWP4.HTM
    • CWP40.HTM
    • CWP41.HTM
    • CWP42.HTM
    • CWP43.HTM
    • CWP44.HTM
    • CWP45.HTM
    • CWP46.HTM
    • CWP47.HTM
    • CWP48.HTM
    • CWP49.HTM
    • CWP5.HTM
    • CWP50.HTM
    • CWP51.HTM
    • CWP52.HTM
    • CWP53.HTM
    • CWP54.HTM
    • CWP55.HTM
    • CWP56.HTM
    • CWP57.HTM
    • CWP58.HTM
    • CWP59.HTM
    • CWP6.HTM
    • CWP60.HTM
    • CWP61.HTM
    • CWP62.HTM
    • CWP63.HTM
    • CWP64.HTM
    • CWP65.HTM
    • CWP66.HTM
    • CWP67.HTM
    • CWP68.HTM
    • CWP69.HTM
    • CWP7.HTM
    • CWP70.HTM
    • CWP71.HTM
    • CWP72.HTM
    • CWP73.HTM
    • CWP74.HTM
    • CWP75.HTM
    • CWP76.HTM
    • CWP77.HTM
    • CWP78.HTM
    • CWP79.HTM
    • CWP8.HTM
    • CWP80.HTM
    • CWP81.HTM
    • CWP82.HTM
    • CWP83.HTM
    • CWP84.HTM
    • CWP85.HTM
    • CWP86.HTM
    • CWP87.HTM
    • CWP88.HTM
    • CWP89.HTM
    • CWP9.HTM
    • CWP90.HTM
    • CWP91.HTM
    • CWP92.HTM
    • CWP93.HTM
    • CWP94.HTM
    • CWP95.HTM
    • CWP96.HTM
    • CWP97.HTM
    • CWP98.HTM
    • CWP99.HTM
    • ImapLibrary.HTM
    • minus.jpg
    • plus.jpg
    • Solution_ImapLibrary.HTM
    • titletile.jpg
    • vt.js
    • ImapLibrary.sln
    • ImapLibrary.sln.DotSettings.user
    • AssemblyInfo.cs
    • Imap.cs
    • ImapBase.cs
    • ImapException.cs
    • ImapLibrary.csproj
    • ImapLibrary.csproj.DotSettings.user
    • ImapLibrary.csproj.user
    • ImapLibrary.csproj.FileListAbsolute.txt
    • App.ico
    • AssemblyInfo.cs
    • ImapLibraryTest.vshost.exe.manifest
    • ImapLibraryTest.csproj
    • ImapLibraryTest.csproj.DotSettings.user
    • ImapLibraryTest.csproj.user
    • ImapLibraryTest.csproj.FileListAbsolute.txt
    • TestImap.cs
    • ImapLibraryTest.png
using System;
namespace Joshi.Utils.Imap
{
	/// <summary>
	/// Imap Exception class which implements Imap releted exceptions
	/// </summary>
	public class ImapException : ApplicationException 
	{
		/// <summary>
		/// Exception message string
		/// </summary>
		string message;
		/// <summary>
		/// enum for Imap exception errors
		/// </summary>
		public enum ImapErrorEnum 
		{
			/// <summary>
			/// failure parsing the url
			/// </summary>
			IMAP_ERR_URI,     
			/// <summary>
			/// invalid message uid in the url
			/// </summary>
			IMAP_ERR_MESSAGEUID,    
			/// <summary>
			/// invalid username/password in the url
			/// </summary>
			IMAP_ERR_AUTHFAILED,   
			/// <summary>
			/// failure connecting to imap server
			/// </summary>
			IMAP_ERR_CONNECT,   
			/// <summary>
			/// not connected to any IMAP
			/// </summary>
			IMAP_ERR_NOTCONNECTED, 
			/// <summary>
			/// failure logging into imap server
			/// </summary>
			IMAP_ERR_LOGIN, 
            /// <summary>
            /// failure to logout from imap server
            /// </summary>
			IMAP_ERR_LOGOUT,  
			/// <summary>
			/// not enough data to restore
			/// </summary>
			IMAP_ERR_INSUFFICIENT_DATA, 
			/// <summary>
			/// timeout while waiting for response
			/// </summary>
			IMAP_ERR_TIMEOUT,
			/// <summary>
			/// socket error while receiving
			/// </summary>
			IMAP_ERR_SOCKET,             
			/// <summary>
			/// failure getting the quota information
			/// </summary>
			IMAP_ERR_QUOTA,              
			/// <summary>
			/// failure selecting a IMAP folder
			/// </summary>
			IMAP_ERR_SELECT,             
			/// <summary>
			/// failure examining an IMAP folder
			/// </summary>
			IMAP_ERR_EXAMINE,            
			/// <summary>
			///  No folder is currently selected
			/// </summary>
			IMAP_ERR_NOTSELECTED,        
			/// <summary>
			/// failure to search
			/// </summary>
			IMAP_ERR_SEARCH,             
			/// <summary>
			/// failed to do exact match after search
			/// </summary>
			IMAP_ERR_SEARCH_EXACT,       
			/// <summary>
			/// unsupported search key
			/// </summary>
			IMAP_ERR_INVALIDSEARCHKEY,   
			/// <summary>
			/// failure to get message MIME
			/// </summary>
			IMAP_ERR_GETMIME,            
			/// <summary>
			/// Message Header is in invalid format
			/// </summary>
			IMAP_ERR_INVALIDHEADER,      
			/// <summary>
			/// Failed to fetch the bodystructure
			/// </summary>
			IMAP_ERR_FETCHBODYSTRUCT,    
			/// <summary>
			/// failure to fetch a IMAP message
			/// </summary>
			IMAP_ERR_FETCHMSG,
            /// <summary>
            /// failure to fetch a IMAP message size
            /// </summary>
            IMAP_ERR_FETCHSIZE,
			/// <summary>
			/// failure to allocate memory
			/// </summary>
			IMAP_ERR_MEMALLOC,           
			/// <summary>
			/// failure to encode the audio content
			/// </summary>
			IMAP_ERR_ENCODINGERROR,      
			/// <summary>
			/// failure to read/write the audio content
			/// </summary>
			IMAP_ERR_FILEIO,             
			/// <summary>
			/// failure to store the message in IMAP
			/// </summary>
			IMAP_ERR_STOREMSG,           
			/// <summary>
			/// failure to issue expunge command
			/// </summary>
			IMAP_ERR_EXPUNGE,            
			/// <summary>
			/// invalid parameter to API
			/// </summary>
			IMAP_ERR_INVALIDPARAM,      
			/// <summary>
			/// Capability command error
			/// </summary>
			IMAP_ERR_CAPABILITY,
			/// <summary>
			/// Serious Problem
			/// </summary>
			IMAP_ERR_SERIOUS             
    
		}
		/// <summary>
		/// Property Message (string)
		/// </summary>
		public override string Message 
		{
			get 
			{
				return message;
			}
			
		}
		/// <summary>
		/// Error Type: ImapErrorEnum
		/// </summary>
        private ImapErrorEnum errorType;
		/// <summary>
		/// Property : Type (ImapErrorEnum)
		/// </summary>
		public ImapErrorEnum Type
		{
			get
			{
				return errorType;
			}
			set
			{
				errorType = value;
			}
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="message">string</param>
		public ImapException (String message) : base (message) 
		{
			this.message = message;
		}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="message">string</param>
		/// <param name="inner">Exception</param>
		public ImapException (String message, Exception inner) : base(message,inner) 
		{
			this.message = message;
		}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="Type">ImapErrorEnum</param>
		public ImapException(ImapErrorEnum Type) 
		{
			errorType = Type;
			message = GetDescription(Type);
		}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="Type">ImapErrorEnum</param>
		/// <param name="error">string</param>
		public ImapException(ImapErrorEnum Type, string error) 
		{
			errorType = Type;
			message = GetDescription(Type);
			message = message + " " + error;
		}
		
		/// <summary>
		/// Get Description for specified Type
		/// </summary>
		/// <param name="Type">ImapErrorEnum type</param>
		/// <returns>string</returns>
		private string GetDescription(ImapErrorEnum Type) 
		{
			switch (Type) 
			{
				case ImapErrorEnum.IMAP_ERR_URI:
					return "Failure parsing the IMAP URL.";
				case ImapErrorEnum.IMAP_ERR_MESSAGEUID:
					return "Invalid UserName/Password in the IMAP URL.";
				case ImapErrorEnum.IMAP_ERR_CONNECT:
					return "Failure connecting to the IMAP server.";
				case ImapErrorEnum.IMAP_ERR_NOTCONNECTED:
					return "Not connected to any IMAP server. Do Login first.";
				case ImapErrorEnum.IMAP_ERR_LOGIN:
					return "Failed authenticating the user/password in the IMAP server.";
				case ImapErrorEnum.IMAP_ERR_INSUFFICIENT_DATA:
					return "Not enough data to Restore the connection.";
				case ImapErrorEnum.IMAP_ERR_LOGOUT:
					return "Failed to Logout from IMAP server.";
				case ImapErrorEnum.IMAP_ERR_TIMEOUT:
					return "Timeout while waiting for response from the IMAP server.";
				case ImapErrorEnum.IMAP_ERR_SOCKET:
					return "Socket Error. Failed receiving message.";
				case ImapErrorEnum.IMAP_ERR_QUOTA:
					return "Failure getting the quota information for the folder/mailbox.";
				case ImapErrorEnum.IMAP_ERR_SELECT:
					return "Failure selecting IMAP folder/mailbox.";
				case ImapErrorEnum.IMAP_ERR_NOTSELECTED:
					return "No Folder is currently selected.";
				case ImapErrorEnum.IMAP_ERR_EXAMINE:
					return "Failure examining IMAP folder/mailbox.";
				case ImapErrorEnum.IMAP_ERR_SEARCH:
					return "Failure searching IMAP with the given criteria.";
				case ImapErrorEnum.IMAP_ERR_SEARCH_EXACT:
					return "Failure getting the exact search value.";
				case ImapErrorEnum.IMAP_ERR_INVALIDSEARCHKEY:
					return "Unsupported search key passed to SearchMessage API.";
				case ImapErrorEnum.IMAP_ERR_GETMIME:
					return "Failure fetching mime for the message.";
				case ImapErrorEnum.IMAP_ERR_FETCHMSG:    
					return "Failure fetching message from IMAP folder/mailbox.";
                case ImapErrorEnum.IMAP_ERR_FETCHSIZE:
                    return "Failure fetching message size from IMAP folder/mailbox.";
				case ImapErrorEnum.IMAP_ERR_MEMALLOC:    
					return "Failure allocating memory.";
				case ImapErrorEnum.IMAP_ERR_ENCODINGERROR:    
					return "Failure encoding audio message.";
				case ImapErrorEnum.IMAP_ERR_FILEIO:    
					return "Failure reading/writing the audio content to file.";
				case ImapErrorEnum.IMAP_ERR_STOREMSG:    
					return "Failure storing message in IMAP.";
				case ImapErrorEnum.IMAP_ERR_EXPUNGE:    
					return "Failure permanently deleting marked messages (EXPUNGE).";
				case ImapErrorEnum.IMAP_ERR_INVALIDPARAM:    
					return "Invalid parameter passed to API. See Log for details";
				case ImapErrorEnum.IMAP_ERR_CAPABILITY:
					return "Failure capability command.";
				case ImapErrorEnum.IMAP_ERR_SERIOUS:    
					return "Serious Problem with IMAP. Contact System Admin.";
				case ImapErrorEnum.IMAP_ERR_FETCHBODYSTRUCT:
					return "Failure bodystructure command";
				default:
					throw new Exception("UnKnow Exception");
				
			}
			
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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.

Comments and Discussions