Using EAGetMail POP3 and IMAP4 UWP Class in Windows 8.1 Store App or UWP (Universal Windows Platform)


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 "Installation Path\Lib\[portable* or uap]\EAGetMail.winmd" 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.

Install from NuGet

You can also install the run-time assembly by NuGet. Run the following command in the NuGet Package Manager Console:

Install-Package EAGetMail

Deploying EAGetMail with Application

After compiling your project, a copy of EAGetMail.winmd will be generated by compiler in same folder of your application executable file. Packing all the *.winmd 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.winmd 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 Windows Run Time

File .NET Framework Version
Lib\portable-win81+wpa81\EAGetMail.winmd Built with .NET Framework 4.5.1
It requires Windows Store App Runtime 8.1 or later version.
Lib\uap10.0\EAGetMail.winmd Built with Universal Windows Platform.
It requires Windows 10 or later version (Universal Windows Platform).

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#, Javascript] The following example demonstrates how to retrieve email and parse email with EAGetMail. To get the full samples of EAGetMail, please refer to Samples section.

[VB - Retrieve email and parse email]
Imports EAGetMail

Async Function ReadParseEmail(
    serverAddress As String,
    user As String,
    password As String,
    sslConnection As Boolean,
    protocol As ServerProtocol) As Task

    Try
        Dim server As MailServer = New MailServer(serverAddress, user, password, protocol)
        server.SSLConnection = sslConnection
        ' Set port based on protocol and ssl.
        Select Case (protocol)
            Case ServerProtocol.Pop3
                If sslConnection Then
                    server.Port = 995
                Else
                    server.Port = 110
                End If

            Case ServerProtocol.Imap4
                If sslConnection Then
                    server.Port = 993
                Else
                    server.Port = 143
                End If
        End Select


        Dim client As MailClient = New MailClient("TryIt")
        Await client.ConnectAsync(server)

        Dim infos As IList(Of MailInfo) = Await client.GetMailInfosAsync()
        For i As Integer = 0 To infos.Count - 1
            Dim info As MailInfo = infos(i)
            Dim mail As Mail = Await client.GetMailAsync(info)
            Dim fileName As String = String.Format("{0}\{1}.eml", 
                Windows.Storage.ApplicationData.Current.LocalFolder.Path, i)
            Await mail.SaveAsAsync(fileName, True)
            Await ParseEmail(fileName)
        Next

        ' delete emails from server.
        For i As Integer = 0 To infos.Count - 1
            Dim info As MailInfo = infos(i)
            Await client.DeleteAsync(info)
        Next

        Await client.QuitAsync()

    Catch ep As Exception
        Dim errorDescrption As String = ep.Message
    End Try

End Function

Async Function ParseEmail(fileName As String) As Task
    Try
        Dim mail As Mail = New Mail("TryIt")
        Await mail.LoadAsync(fileName)

        Dim sender As String = mail.From.Address
        Dim subject As String = mail.Subject

        Dim [to] As IList(Of MailAddress) = mail.To
        For i As Integer = 0 To [to].Count - 1
            Dim address As String = [to](i).Address
        Next

        Dim cc As IList(Of MailAddress) = mail.Cc
        For i As Integer = 0 To [cc].Count - 1
            Dim address As String = cc(i).Address
        Next

        Dim textBody As String = mail.TextBody
        Dim htmlBody As String = mail.HtmlBody
        Dim attachments As IList(Of Attachment) = mail.Attachments
        For i As Integer = 0 To attachments.Count - 1
            Dim attachment As Attachment = attachments(i)
            Dim attachmentName As String = String.Format("{0}\{1}",
                Windows.Storage.ApplicationData.Current.LocalFolder.Path, attachment.Name)
            Await attachment.SaveAsAsync(attachmentName, True)
        Next

    Catch ep As Exception
        Dim errorDescrption As String = ep.Message
    End Try


End Function

[C# - Retrieve and parse email]
using EAGetMail;

async Task ReadParseEmail( string serverAddress,
    string user, 
    string password,
    bool sslConnection,
    ServerProtocol protocol)
{
    try
    {
        MailServer server = new MailServer(serverAddress, user, password, protocol);
        server.SSLConnection = sslConnection;

        // Set port based on protocol and ssl.
        switch (protocol)
        {
            case ServerProtocol.Pop3:
                server.Port = (sslConnection) ? 995 : 110;
                break;
            case ServerProtocol.Imap4:
                server.Port = (sslConnection) ? 993 : 143;
                break;
            default:
                break;
        }

        MailClient client = new MailClient("TryIt");
        await client.ConnectAsync(server);

        IList infos = await client.GetMailInfosAsync();
        for (int i = 0; i < infos.Count; i++)
        {
            MailInfo info = infos[i];
            Mail mail = await client.GetMailAsync(info);

            string fileName = String.Format("{0}\\{1}.eml",
                Windows.Storage.ApplicationData.Current.LocalFolder.Path, i);

            await mail.SaveAsAsync(fileName, true);

            await ParseEmail(fileName);
        }

        // delete emails from server.
        for (int i = 0; i < infos.Count; i++)
        {
            MailInfo info = infos[i];
            await client.DeleteAsync(info);
        }

        await client.QuitAsync();

    }
    catch (Exception ep)
    {
        string errorDescrption = ep.Message;
    }
}

async Task ParseEmail(string fileName)
{
    try
    {
        Mail mail = new Mail("TryIt");
        await mail.LoadAsync(fileName);
        string sender = mail.From.Address;
        string subject = mail.Subject;

        IList<MailAddress> to = mail.To;
        for (int i = 0; i < to.Count; i++)
        {
            string address = to[i].Address;
        }

        IList<MailAddress> cc = mail.Cc;
        for (int i = 0; i < cc.Count; i++)
        {
            string address = cc[i].Address;
        }

        string textBody = mail.TextBody;
        string htmlBody = mail.HtmlBody;

        IList<Attachment> attachments = mail.Attachments;
        for (int i = 0; i < attachments.Count; i++)
        {
            Attachment attachment = attachments[i];
            string attachmentName = String.Format("{0}\\{1}",
                Windows.Storage.ApplicationData.Current.LocalFolder.Path, attachment.Name);

            await attachment.SaveAsAsync(attachmentName, true);
        }

    }
    catch (Exception ep)
    {
        string errorDescrption = ep.Message;
    }
}

[Javascript - Retrieve and parse email]

function readParseEmail(serverAddress,
    user,
    password,
    sslConnection,
    protocol) {

    try {
        var server = new EAGetMail.MailServer(serverAddress, user, password, protocol);
        server.sslconnection = sslConnection;

        switch (protocol)
        {
            case EAGetMail.ServerProtocol.pop3:
                server.port = sslConnection ? 995 : 110;
            break;
            case EAGetMail.ServerProtocol.imap4:
                server.port = sslConnection ? 993 : 143;
            break;
        default:
            break;
        }

        var client = new EAGetMail.MailClient("TryIt");

        client.connectAsync(server)
        .then(function () {
            return client.getMailInfosAsync();
        })
        .then(function (infos) {
            var promise = WinJS.Promise.as();
            
            infos.forEach(function (info, index) {
                promise = promise
                .then(function () {
                    return client.getMailAsync(info);
                })
                .then(function (mail) {
                    var fileName = Windows.Storage.ApplicationData.current.localFolder.path
                        + '\\' + index + '.eml';
                    return mail.saveAsAsync(fileName, true)
                    .then(function () {
                        return WinJS.Promise.as(fileName);
                    });
                })
                .then(function (fileName) {
                    return parseEmail(fileName);
                });

            });

            return promise;

        })
        .then(function () {
            return client.quitAsync();
        })
        .done(function () {
        },

        function (e) {
            var errorDescription = e.description;
        }
        );

    }
    catch (e)
    {
        var errorDescription = e.description;
    }
}

function parseEmail(fileName) {

    var mail = new EAGetMail.Mail('TryIt');
    return mail.loadAsync(fileName)
    .then(function () {
        var sender = mail.from.address;
        var subject = mail.subject;
        var to = mail.to;
        for (var i = 0; i < to.length; i++) {
            var address = to[i].address;
        }

        var cc = mail.cc;
        for (var i = 0; i < cc.length; i++) {
            var address = cc[i].address;
        }

        var textBody = mail.textBody;
        var htmlBody = mail.htmlBody;

        var attachments = mail.attachments;

        var promise = WinJS.Promise.as();
        attachments.forEach(function (attachment, index) {
            promise = promise.then(function () {
                var file = Windows.Storage.ApplicationData.current.localFolder.path + '\\' + attachment.name;
                return attachment.saveAsAsync(file, true);
            });
        });

        return promise;
    });
}