Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

IMAP and POP3 Clients in C#

4.67/5 (21 votes)
28 Sep 2012CPOL1 min read 263.7K   16.6K  
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)