Using EAGetMail POP3 and IMAP4 .NET Class


Add Reference of EAGetMail to Visual Stuido.NET Project

To use EAGetMail POP3 and IMAP Component in your project, the first step is "Add reference of EAGetMail to your project". Please create/open your project with Visual Studio.NET, then choose menu->"Project"->"Add Reference"->".NET"->"Browse...", and choose the EAGetMail.dll from your disk, click "Open"->"OK", the reference of EAGetMail will be added to your project, and you can start to use EAGetMail POP3 and IMAP Component in your project.

add reference of EAGetMail in c#

Deploying EAGetMail with Application

After compiling your project, a copy of EAGetMail.dll will be generated by compiler in same folder of your application executable file. Packing all the *.dll and *.exe in the folder to installer is ok. As EAGetMail is a pure .NET Component, it doesn't require "Regsvr32" (self-register) to register the dll.

Deploying EAGetMail with ASP.NET/Web Application

The EAGetMail.dll should be copied to [website root folder]\bin folder or [virtual directory root]\bin folder. If the project is created by Visual Studio.NET + FrontPage Extension directly, Visual Studio.NET will deploy EAGetMail.dll automatically.

Seperate builds of run-time assembly for .Net Framework 1.1, 2.0, 3.5, 4.0, 4.5 and .Net Compact Framework 2.0, 3.5.

File .NET Framework Version
EAGetMail.dll Built with .NET Framework 1.1
It requires .NET Framework 1.1, 2.0, 3.5 or later version.
EAGetMail20.dll Built with .NET Framework 2.0
It requires .NET Framework 2.0, 3.5 or later version.
EAGetMail35.dll Built with .NET Framework 3.5
It requires .NET Framework 3.5 or later version.
EAGetMail40.dll Built with .NET Framework 4.0
It requires .NET Framework 4.0 or later version.
EAGetMail45.dll Built with .NET Framework 4.5
It requires .NET Framework 4.5 or later version.
EAGetMailCF20.dll Built with .NET Compact Framework 2.0
It requires .NET Compact Framework 2.0, 3.5 or later version.
EAGetMailCF35.dll Built with .NET Compact Framework 3.5
It requires .NET Compact Framework 3.5 or later version.

Run-time library for .NET Compact Framework 2.0, 3.5

To use EAGetMail in .NET Compact Framework 2.0/3.5, you should use EAGetMailCF20.dll/EAGetMailCF35.dll instead of EAGetMail.dll in your project. And if SSL connection is used in .NET Compact Framework 2.0/3.5, you should also copy "SecurityInterface.dll" to the same folder of EAGetMailCF20.dll/EAGetMailCF35.dll.

Note: EAGetMailCF20/EAGetMailCF35 does not support digital signature verification and message decryption.

Send/Edit/Compose Email

EAGetMail Component doesn't send, edit or compose email. To send, edit or compose email, please use EASendMail SMTP Component.

Example

[Visual Basic, C#, C++] The following example demonstrates how to receive email with EAGetMail POP3 & IMAP Component, but it doesn't demonstrates the events and mail parsing usage. To get the full samples of EAGetMail, please refer to Samples section.

[VB - Retrieve Email from Server]
Imports EAGetMail

Public Sub ReceiveMail( _
ByVal sServer As String, _
ByVal sUserName As String, _
ByVal sPassword As String, _
ByVal bSSLConnection As Boolean)
    Dim oClient As New MailClient("TryIt")
    
    'To receive email from imap4 server, please change
    'ServerProtocol.Pop3 to ServerProtocol.Imap4 in MailServer constructor

    'To receive email with Exchange Web Service, please change
    'ServerProtocol.Pop3 to ServerProtocol.ExchangeEWS to MailServer constructor

    'To receive email with Exchange WebDAV, please change
    'ServerProtocol.Pop3 to ServerProtocol.ExchangeWebDAV to MailServer constructor

    'Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007
    'or later version, POP3/IMAP4 service is disabled by default. If you don't want to use POP3/IMAP4
    'to download email from Exchange Server, you can use Exchange Web Service (Exchange 2007/2010 or
    'later version) or WebDAV (Exchange 2000/2003) protocol.

    'For Exchange Web Service/WebDAV, please ignore 
    'Port property. But for Exchange Web Service, please set SSLConnection to True
    
    Dim oServer As New MailServer(sServer, _
        sUserName, sPassword, bSSLConnection, _
        ServerAuthType.AuthLogin, ServerProtocol.Pop3)

    'by default, the pop3 port is 110, imap4 port is 143, 
    'the pop3 ssl port is 995, imap4 ssl port is 993
    'you can also change the port like this
    'oServer.Port = 110
    Try
        oClient.Connect(oServer)
        Dim infos() As MailInfo = oClient.GetMailInfos()
        Dim count As Integer = infos.Length
        For i As Integer = 0 To count - 1
            Dim info As MailInfo = infos(i)
            Dim oMail As Mail = oClient.GetMail(info)
            ''Save mail to local    file
            oMail.SaveAs(String.Format("c:\{0}.eml", i), True)
        Next

        For i As Integer = 0 To count - 1
            Dim info As MailInfo = infos(i)
            oClient.Delete(info)
        Next
        '
        ' Delete method just mark the email as deleted, 
        ' Quit method purge the emails from server exactly.
        oClient.Quit()

    Catch ep As MailServerException
        ''Message contains the information returned by mail server
        Console.WriteLine("Server Respond: {0}", ep.Message)
    Catch ep As System.Net.Sockets.SocketException
        Console.WriteLine("Socket Error: {0}", ep.Message)
    Catch ep As Exception
        Console.WriteLine("System Error: {0}", ep.Message)
    End Try

    oClient.Close()
End Sub

[C# - Retrieve Email from Server]
using System;
using System.Collections;
using EAGetMail;

public void ReceiveMail( 
    string sServer, 
    string sUserName,
    string sPassword,
    bool bSSLConnection)
{
    MailClient oClient = new MailClient("TryIt");
     //To receive email from imap4 server, please change
    //ServerProtocol.Pop3 to ServerProtocol.Imap4 in MailServer constructor

    //To receive email with Exchange Web Service, please change
    //ServerProtocol.Pop3 to ServerProtocol.ExchangeEWS to MailServer constructor

    //To receive email with Exchange WebDAV, please change
    //ServerProtocol.Pop3 to ServerProtocol.ExchangeWebDAV to MailServer constructor

    //Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007
    //or later version, POP3/IMAP4 service is disabled by default. If you don//t want to use POP3/IMAP4
    //to download email from Exchange Server, you can use Exchange Web Service (Exchange 2007/2010 or
    //later version) or WebDAV (Exchange 2000/2003) protocol.

    //For Exchange Web Service/WebDAV, please ignore 
    //Port property. But for Exchange Web Service, please set SSLConnection to true
    

    MailServer oServer  = new MailServer(sServer, 
        sUserName, sPassword, bSSLConnection, 
        ServerAuthType.AuthLogin, ServerProtocol.Pop3);

    //by default, the pop3 port is 110, imap4 port is 143, 
    //the pop3 ssl port is 995, imap4 ssl port is 993
    //you can also change the port like this
    //oServer.Port = 110;
    try
    {
        oClient.Connect(oServer);
        MailInfo [] infos = oClient.GetMailInfos();
        int count = infos.Length;
        for( int i = 0; i < count; i++ )
        {
            MailInfo info = infos[i];
            Mail oMail = oClient.GetMail(info);
            //Save mail to local file
            oMail.SaveAs(String.Format("c:\\{0}.eml", i), true);
        }

        for( int i = 0; i < count; i++ )
        {
            MailInfo info = infos[i];
            oClient.Delete(info);
        }
    
        // Delete method just mark the email as deleted, 
        // Quit method purge the emails from server exactly.
        oClient.Quit();
    }
    catch( MailServerException ep ) 
    {
        //Message contains the information returned by mail server
        Console.WriteLine("Server Respond: {0}", ep.Message);
    }
    catch( System.Net.Sockets.SocketException ep ) 
    {
        Console.WriteLine("Socket Error: {0}", ep.Message);
    }
    catch( Exception ep ) 
    {
        Console.WriteLine("System Error: {0}", ep.Message);
    }

    oClient.Close();
}

[C++/CLI - Retrieve Email from Server]
using namespace System;
using namespace System::Collections;
using namespace EAGetMail; 

void ReceiveMail( 
        String ^sServer, 
        String ^sUserName,
        String ^sPassword,
        bool bSSLConnection)
{
    MailClient ^oClient = gcnew MailClient("TryIt");
    //To receive email from imap4 server, please change
    //ServerProtocol::Pop3 to ServerProtocol::Imap4 in MailServer constructor

    //To receive email with Exchange Web Service, please change
    //ServerProtocol::Pop3 to ServerProtocol::ExchangeEWS to MailServer constructor

    //To receive email with Exchange WebDAV, please change
    //ServerProtocol::Pop3 to ServerProtocol::ExchangeWebDAV to MailServer constructor

    //Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007
    //or later version, POP3/IMAP4 service is disabled by default. If you don//t want to use POP3/IMAP4
    //to download email from Exchange Server, you can use Exchange Web Service (Exchange 2007/2010 or
    //later version) or WebDAV (Exchange 2000/2003) protocol.

    //For Exchange Web Service/WebDAV, please ignore 
    //Port property. But for Exchange Web Service, please set SSLConnection to true

    MailServer ^oServer  = gcnew MailServer(sServer, 
        sUserName, sPassword, bSSLConnection, 
        ServerAuthType::AuthLogin, ServerProtocol::Pop3);

    //by default, the pop3 port is 110, imap4 port is 143, 
    //the pop3 ssl port is 995, imap4 ssl port is 993
    //you can also change the port like this
    //oServer->Port = 110;
    try
    {
        oClient->Connect(oServer);
        array<MailInfo^>^infos = oClient->GetMailInfos();
        int count = infos->Length;
        for( int i = 0; i < count; i++ )
        {
            MailInfo ^info = infos[i];
            Mail ^oMail = oClient->GetMail(info);
            //Save mail to local file
            oMail->SaveAs(String::Format("c:\\{0}.eml", i), true);
        }

        for( int i = 0; i < count; i++ )
        {
            MailInfo ^info = infos[i];
            oClient->Delete(info);
        }
    
        // Delete method just mark the email as deleted, 
        // Quit method purge the emails from server exactly.
        oClient->Quit();
    }
    catch( MailServerException ^ep ) 
    {
        //Message contains the information returned by mail server
        Console::WriteLine( "Server Respond: {0}", ep->Message);
    }
    catch( Exception ^ep ) 
    {
        Console::WriteLine( "System Error: {0}", ep->Message);
    }

    oClient->Close();
}

See Also

User Authentication and SSL Connection
Digital Signature and E-mail Encryption/Decryption
Unique Identifier (UIDL) in POP3 and IMAP4 protocol
Parse Bounced Email (delivery-report)
Work with winmail.dat (TNEF Parser)
EAGetMail Namespace References
EAGetMail POP3 & IMAP4 Component Samples

Online Tutorials

Read Email and Parse Email in C# - Tutorial
Read Email and Parse Email in VB.NET - Tutorial
Read Email and Parse Email C++/CLI - Tutorial