Download Email with Background Service in Delphi

In previous section, I introduced how to use UIDL function to mark the email has been downloaded. In this section, I will introduce how to download email with background service in Delphi.


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 POP3 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.


To run the following example, you need to download EAGetMail and EAGetMail Service at first, and then install both on your machine.


Remarks: All of examples in this section are based on first section: A simple Delphi project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference to your project.

[Delphi Example - Retrieve email with background service]

The following example codes demonstrate how to use EAGetMail POP3 component to retrieve email with background service. In order to run it correctly, please change email server, user, password, folder, file name values.


To get the full sample projects, please refer to Samples section.

unit Unit1;


    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, EAGetMailObjLib_TLB;

    TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        { Private declarations }
        { Public declarations }

    MailServerPop3 = 0;
    MailServerImap4 = 1;
    MailServerEWS = 2;
    MailServerDAV = 3;
    MailServerMsGraph = 4;

    Form1: TForm1;


{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
    oServer: TMailServer;
    oClient: TMailClient;
    oTools: TTools;
    localInbox: WideString;
    leaveCopy: Boolean;

        // set current thread code page to system default code page.
        oTools := TTools.Create(Application);

        // Create a folder named "inbox" under
        // current directory to store the email files
        localInbox := GetCurrentDir() + '\inbox';

        oServer := TMailServer.Create(Application);
        oServer.Server := '';
        oServer.User := '';
        oServer.Password := 'testpassword';
        oServer.Protocol := MailServerPop3;

        // Enable SSL/TLS connection, most modern email servers require SSL/TLS by default
        oServer.SSLConnection := true;
        oServer.Port := 995;

        // If your POP3 doesn't deploy SSL connection
        // Please use
        // oServer.SSLConnection := false;
        // oServer.Port := 110;

        // Leave a copy of message
        leaveCopy := true;

        oClient := TMailClient.Create(Application);
        oClient.LicenseCode := 'TryIt';

        // Send request to EAGetMail Service, then EAGetMail Service retrieves email
        // in background and this method returns immediately.
        oClient.GetMailsByQueue(oServer.DefaultInterface, localInbox, leaveCopy);

        on ep:Exception do
            ShowMessage('Error: ' + 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.

Next Section

At next section I will introduce how to parse email in Delphi.



If you have any comments or questions about above example codes, please click here to add your comments.