Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#
Tip/Trick

IMAP and POP3 Clients in C#

Rate me:
Please Sign up or sign in to vote.
4.67/5 (21 votes)
28 Sep 2012CPOL1 min read 257.1K   16.6K   48   62
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.

C#
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

C#
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.

C#
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

C#
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)


Written By
Software Developer (Senior) D.Net Solution
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDoes not read the attachment filename Pin
Member 1194702115-Nov-17 21:52
Member 1194702115-Nov-17 21:52 
Questionany way to delete an IMAP email message? Pin
dileepawijay6-Jan-16 6:15
dileepawijay6-Jan-16 6:15 
Questioncan anyone help me to use it? Pin
antani7421-Jul-15 4:47
antani7421-Jul-15 4:47 
AnswerRe: can anyone help me to use it? Pin
antani7422-Jul-15 0:51
antani7422-Jul-15 0:51 
GeneralRe: can anyone help me to use it? Pin
antani7422-Jul-15 2:40
antani7422-Jul-15 2:40 
GeneralRe: can anyone help me to use it? Pin
Dolby.Xiaowen23-Jul-15 23:43
Dolby.Xiaowen23-Jul-15 23:43 
GeneralRe: can anyone help me to use it? Pin
antani7430-Jul-15 0:21
antani7430-Jul-15 0:21 
GeneralRe: can anyone help me to use it? Pin
Dolby.Xiaowen31-Jul-15 0:40
Dolby.Xiaowen31-Jul-15 0:40 
QuestionDelete message Pin
rikdepeuter20-Jul-15 23:59
rikdepeuter20-Jul-15 23:59 
QuestionPlease help me to use it Pin
Sara NA30-Jun-15 22:49
Sara NA30-Jun-15 22:49 
AnswerRe: Please help me to use it Pin
antani7421-Jul-15 4:46
antani7421-Jul-15 4:46 
QuestionMarking Mails as Read Pin
Sa'eed Namdar21-Jun-15 21:06
Sa'eed Namdar21-Jun-15 21:06 
AnswerRe: Marking Mails as Read Pin
yang2006010930-Sep-15 0:00
yang2006010930-Sep-15 0:00 
GeneralUsing this library for Email Client Pin
Sagar9J8728-May-15 21:54
Sagar9J8728-May-15 21:54 
GeneralMy vote of 3 Pin
Afzaal Ahmad Zeeshan20-Aug-14 9:57
professionalAfzaal Ahmad Zeeshan20-Aug-14 9:57 
QuestionRich Text / HTML Pin
Jeff Dressing10-May-14 5:23
Jeff Dressing10-May-14 5:23 
QuestionLineSizeExceededException Pin
Stein The Ruler5-Mar-14 2:23
Stein The Ruler5-Mar-14 2:23 
AnswerRe: LineSizeExceededException Pin
D.Net Solutions27-Mar-14 6:49
D.Net Solutions27-Mar-14 6:49 
GeneralRe: LineSizeExceededException Pin
Stein The Ruler2-May-14 9:42
Stein The Ruler2-May-14 9:42 
Questionthere is an Error Pin
heemanshubhalla10-Feb-14 2:52
heemanshubhalla10-Feb-14 2:52 
Questionplease give instructions to operate Pin
heemanshubhalla10-Feb-14 1:04
heemanshubhalla10-Feb-14 1:04 
QuestionCan't use - "Does not exist in the current context"? Pin
gajit13-Nov-13 4:05
gajit13-Nov-13 4:05 
QuestionIMAP attachements from wrong emails Pin
jaboostinky6-Oct-13 14:51
jaboostinky6-Oct-13 14:51 
QuestionStato Messaggio Pin
Z@clarco4-Sep-13 3:44
Z@clarco4-Sep-13 3:44 
AnswerRe: Stato Messaggio Pin
D.Net Solutions21-Nov-13 2:45
D.Net Solutions21-Nov-13 2:45 

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.