Work with EAGetMail Service - Download Email in Background Service


EAGetMail Service is an advanced service which can download emails from POP3/IMAP4/Exchange servers in background. It provides many features such as retrieving and saving emails to specified folders in schedule, and starting a specified application to handle downloaded emails.

This service facilitates EAGetMail POP3/IMAP4 Component developers that their applications can now accept an email retrieving request from EAGetMail Application, and then download emails in background. This is particularly useful for ASP/ASP.NET web application.

Retrieve emails in background

In an ASP/ASP.NET email application, if email download takes longer than the timeout value (in seconds) that the current asp page is allowed to run, user will get an error "ASP Timeout". This often happens when user has a large quantity of emails to download or user downloads emails via a slow connection. By default the timeout is set to 90 seconds, it is easy to exceed this limit.

Obviously, a solution to ASP timeout is to set ASPScriptTimeout a larger value. You may click here for details. Technically the timeout problem can be solved in this way; however, some users may get frustrated and press the stop button on the browser toolbar as he waits too long.

EAGetMail Component introduces a more intelligent method to retrieve emails in background. You should download the EAGetMail Service installer and install it on your machine at first. Then you can use MailClient.GetMailsByQueue method to send the request to EAGetMail Service, the method returns to the user immediately and the EAGetMail Service performs task in background.

Example

[Visual Basic]
Imports System
Imports System.Globalization
Imports System.IO
Imports System.Text
Imports EAGetMail

Public Sub ReceiveMailByQueue(server As String, user As String, password As String, useSsl As Boolean)

    Try
        ' Create a folder named "inbox" under current directory
        ' to save the email retrieved.
        Dim localInbox As String = String.Format("{0}\inbox", Directory.GetCurrentDirectory())

        ' If the folder is not existed, create it.
        If Not Directory.Exists(localInbox) Then
            Directory.CreateDirectory(localInbox)
        End If

        ' 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-2019 Or
        ' later version) Or WebDAV (Exchange 2000/2003) protocol.

        ' Most modern email server require SSL/TLS connection, 
        ' set useSsl to true Is recommended.
        Dim oServer As New MailServer(server, user, password, useSsl,
                    ServerAuthType.AuthLogin, ServerProtocol.Pop3)

        ' POP3 port Is 110, POP3 SSL port Is 995
        ' IMAP4 port Is 143,  IMAP4 SSL port Is 993.
        ' EWS/WebDAV, please ignore Port property.
        If oServer.Protocol = ServerProtocol.Pop3 Then
            oServer.Port = If(useSsl, 995, 110)
        ElseIf oServer.Protocol = ServerProtocol.Imap4 Then
            oServer.Port = If(useSsl, 993, 143)
        End If

        Dim oClient As New MailClient("TryIt")

        ' Leave a copy of message on server.
        Dim leaveCopy As Boolean = True

        ' Download emails to this local folder
        Dim downloadFolder As String = localInbox

        ' Send request to EAGetMail Service, then EAGetMail Service retrieves email
        ' in background and this method returns immediately.
        oClient.GetMailsByQueue(oServer, downloadFolder, leaveCopy)

        Console.WriteLine("Completed!")
    Catch ep As Exception
        Console.WriteLine("Error: {0}", ep.Message)
    End Try

End Sub   


[C#] using System; using System.IO; using System.Globalization; using System.Text; using EAGetMail; public void ReceiveMailByQueue(string server, string user, string password, bool useSsl) { try { // Create a folder named "inbox" under current directory // to save the email retrieved. string localInbox = string.Format("{0}\\inbox", Directory.GetCurrentDirectory()); // If the folder is not existed, create it. if (!Directory.Exists(localInbox)) { Directory.CreateDirectory(localInbox); } // 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-2019 or // later version) or WebDAV (Exchange 2000/2003) protocol. // Most modern email server require SSL/TLS connection, // set useSsl to true is recommended. MailServer oServer = new MailServer(server, user, password, useSsl, ServerAuthType.AuthLogin, ServerProtocol.Pop3); // POP3 port is 110, POP3 SSL port is 995 // IMAP4 port is 143, IMAP4 SSL port is 993. // EWS/WebDAV, please ignore Port property. if (oServer.Protocol == ServerProtocol.Pop3) { oServer.Port = (useSsl) ? 995 : 110; } else if (oServer.Protocol == ServerProtocol.Imap4) { oServer.Port = (useSsl) ? 993 : 143; } MailClient oClient = new MailClient("TryIt"); // Leave a copy of message on server. bool leaveCopy = true; // Download emails to this local folder string downloadFolder = localInbox; // Send request to EAGetMail Service, then EAGetMail Service retrieves email // in background and this method returns immediately. oClient.GetMailsByQueue(oServer, downloadFolder, leaveCopy); Console.WriteLine("Completed!"); } catch (Exception ep) { Console.WriteLine("Error: {0}", ep.Message); } }

Scheduled to retrieve emails

EAGetMail Service provides another advanced feature which allow you set a POP3/IMAP4 account in the EAGetMail Service Manager. EAGetMail Service can download emails from this account in specified time interval. If you need to process the email in the mailbox on a regular basis, you just need to create your mail process application, you don't need to write your email download task part. Finally you just need to set your mail account in EAGetMail Service Manager and specify your application or COMMAND, EAGetMail service will download the emails and invoke your application automatically. Please refer to Mail Pull for more detail.

I also suggest that you have a look at Parse Non-Delivery Report (NDR) using EAGetMail Service. It demonstrates how to set a schedule to check a mailbox and insert non-delivery report to SQL database.

See Also

Using EAGetMail POP3 and IMAP4 Component
User Authentication and SSL Connection
Enable TLS 1.2 on Windows XP/2003/2008/7/2008 R2
Using Gmail IMAP4 OAUTH
Using Gmail/GSuite Service Account + IMAP4 OAUTH
Using Office365 EWS OAUTH
Using Office365 EWS OAUTH in Background Service
Using Hotmail IMAP4 OAUTH
Digital Signature and E-mail Encryption/Decryption
Parse Bounced Email (delivery-report)
Work with winmail.dat (TNEF Parser)
EAGetMail Namespace References
EAGetMail POP3 and IMAP4 Component Samples

Online Tutorials

Download Email with Background Service in C# - Tutorial
Download Email with Background Service in VB.NET - Tutorial
Download Email with Background Service in C++/CLI - Tutorial