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

IMAP and POP3 Clients in C#

, 28 Sep 2012
Rate this:
Please Sign up or sign in to vote.
IMAP & POP3 Clients C#. A library for intuitive ease of use of these two protocols.

Introduction 

Searching on the web I was never able to find a library simple and effective way to manage email client with IMAP or POP3 without having to create a good wrapper.

So I decided to make available to the wrapper that is currently used to handle these two protocols. 

Background 

This library is a library wrapper of free Limisoft library (attached in the download). Lumisoft web site

This library allows you to create a client or IMAP or POP3 is 100% compatible with servers such as:

  • Gmail
  • Windows Live

Servers Libero, AOL, Exchange, Yahoo, etc.. are still in testing and not sure the 100% working. The POP3 protocol is implemented based on RFC 1939 Link .

The IMAP protocol is implemented based on the standard Internet Message Access Protocol - Version 4 rev1 Link .

Using the code

This simple library to be able to create basic client using the two protocols used in the web: IMAP and POP3

The library exposes two interfaces: IMailClient and IMail.

public interface IEmail 
{
}

public interface IEmailClient 
{ 
}

IEmail is an instance of an email message on server. IEmailClient is an instance of a POP3 or IMAP client.

To construct an instance of a client to be used the factory "EmailClientFactory" passing the method "GetClient" the type of protocol you want to use

public static class EmailClientFactory
{ 
    public static IEmailClient GetClient(EmailClientEnum type)
    {
        if (type == EmailClientEnum.IMAP) return new IMAP_Wrapper();
        if (type == EmailClientEnum.POP3) return new POP3_Wrapper();
        return null;
    } 
}

The interface IMailClient allows to manage the clients in the same way.

public interface IEmailClient
{
    event EmailClient_OnMessagesLoaded OnMessagesLoaded;
    bool IsConnected
    {
        get;
        set;
    }
    List<IEmail> Messages
    {
        get;
        set;
    }
    void Connect(String server, String User, String pass, int port, bool useSSl);
    void Disconnect();
    void SetCurrentFolder(String folder);
    int GetMessagesCount();
    void LoadMessages();
    void LoadMessages(String start, String end);
    void LoadRecentMessages(Int32 lastSequenceNumber);
}

Using the library

IEmailClient ImapClient = EmailClientFactory.GetClient(EmailClientEnum.IMAP); 
ImapClient.Connect("imap.gmail.com", "example@gmail.com", "examplePass", 993, true);    
ImapClient.SetCurrentFolder("INBOX"); 
// I assume that 5 is the last "ID" readed by my client. 
// If I want to read all messages i use "ImapClient.LoadMessages();"
ImapClient.LoadRecentMessages(5);
// To read all my messages loaded:
for (int i = 0; i < ImapClient.Messages.Count; i++)
{
    IEmail msm = (IEmail)ImapClient.Messages[i];
    // Load all infos include attachments
    msm.LoadInfos();
    Console.Writeline(msm.Date.ToString() + " - " + msm.From[0] + " - " + 
                      msm.Subject + " - " + msm.Attachments.Count);
}

History 

27/09/2012 : First version 1.0.0.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

D.Net Solutions
Software Developer (Senior) D.Net Solution
Italy Italy
No Biography provided
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionRich Text / HTML PinmemberJeff Dressing10-May-14 5:23 
QuestionLineSizeExceededException Pinmembersteinster5-Mar-14 2:23 
AnswerRe: LineSizeExceededException PinmemberD.Net Solutions27-Mar-14 6:49 
GeneralRe: LineSizeExceededException Pinmembersteinster2-May-14 9:42 
Questionthere is an Error Pinmemberheemanshubhalla10-Feb-14 2:52 
Questionplease give instructions to operate Pinmemberheemanshubhalla10-Feb-14 1:04 
QuestionCan't use - "Does not exist in the current context"? Pinmembergajit13-Nov-13 4:05 
QuestionIMAP attachements from wrong emails Pinmemberjaboostinky6-Oct-13 14:51 
QuestionStato Messaggio PinmemberZ@clarco4-Sep-13 3:44 
AnswerRe: Stato Messaggio PinmemberD.Net Solutions21-Nov-13 2:45 
GeneralMy vote of 4 PinmemberSgtPooki7-Jun-13 5:21 
QuestionHTML PinmemberJimboStink17-Apr-13 15:30 
QuestionEmail not read in message inbox PinmemberWilhelm Meier18-Dec-12 8:01 
Questionnot read emails PinmemberWilhelm Meier18-Dec-12 2:14 
AnswerRe: not read emails PinmemberD.Net Solutions18-Dec-12 3:57 
GeneralRe: not read emails PinmemberWilhelm Meier18-Dec-12 5:38 
GeneralRe: not read emails PinmemberWilhelm Meier18-Dec-12 5:41 
GeneralRe: not read emails PinmemberD.Net Solutions18-Dec-12 21:17 
GeneralRe: not read emails PinmemberWilhelm Meier19-Dec-12 2:21 
Questioncan you provide some guide line PinmemberSusheelbn15-Dec-12 0:17 
AnswerRe: can you provide some guide line PinmemberD.Net Solutions17-Dec-12 2:14 
QuestionDelete email PinmemberBitteron13-Dec-12 21:31 
AnswerRe: Delete email PinmemberD.Net Solutions13-Dec-12 23:40 
You must do it for your self for now.
 
Next release i'll add modify and delete messages
QuestionRe: Delete email Pinmemberlevelboy11-Jul-13 1:56 
QuestionI am a newbie here and need help with this PinmemberGPS Waraich13-Dec-12 2:50 
AnswerRe: I am a newbie here and need help with this PinmemberD.Net Solutions13-Dec-12 23:42 
GeneralMy vote of 4 Pinmembermansoor.siddique14-Nov-12 4:49 
GeneralAttachments Pinmemberantonio_l4-Oct-12 3:07 
GeneralRe: Attachments PinmemberD.Net Solutions4-Oct-12 21:19 
GeneralRe: Attachments Pinmemberantonio_l5-Oct-12 0:38 
QuestionProblem using this library PinmemberMember 392317128-Sep-12 2:58 
AnswerRe: Problem using this library PinmemberD.Net Solution28-Sep-12 3:04 
GeneralRe: Problem using this library PinmemberMember 392317128-Sep-12 3:17 
GeneralRe: Problem using this library PinmemberD.Net Solution28-Sep-12 3:20 
GeneralRe: Problem using this library PinmemberMember 775724528-Sep-12 8:43 
GeneralRe: Problem using this library PinmemberD.Net Solutions2-Oct-12 0:38 
GeneralRe: Problem using this library PinmemberMember 77572452-Oct-12 9:20 
QuestionQuery regarding using this Code Pinmemberhirenel28-Sep-12 1:00 
AnswerRe: Query regarding using this Code PinmemberD.Net Solution28-Sep-12 2:10 
QuestionGreat stuff PinmemberSorenDalby27-Sep-12 23:33 
AnswerRe: Great stuff PinmemberD.Net Solution27-Sep-12 23:43 
GeneralRe: Great stuff PinmemberMember 775724528-Sep-12 8:44 
GeneralRe: Great stuff PinmemberD.Net Solutions2-Oct-12 0:42 
GeneralRe: Great stuff PinmemberMember 77572452-Oct-12 9:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140721.1 | Last Updated 28 Sep 2012
Article Copyright 2012 by D.Net Solutions
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid