Retrieve Email with Event Handler in Delphi

In previous section, I introduced how to retrieve email from Hotmail/MSN Live account. In this section, I will introduce how to retrieve email with event handler in Delphi.

Introduction

After Connect method, GetMail method or other methods are invoked, if you want to know the progress of the email receiving, you should use Event Handler. The following sample codes demonstrate how to use Event Handler to monitor the progress of email receiving.

Note

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 event handler]

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

Note

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

unit Unit1;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, EAGetMailObjLib_TLB; // Add EAGetMail unit

type
    TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        procedure Button1Click(Sender: TObject);

        // EAGetMail event handler
        procedure OnIdle(ASender: TObject; const oSender: IDispatch; var Cancel: WordBool);
        procedure OnConnected(ASender: TObject; const oSender: IDispatch; var Cancel: WordBool);
        procedure OnQuit(ASender: TObject; const oSender: IDispatch; var Cancel: WordBool);
        procedure OnSecuring(ASender: TObject; const oSender: IDispatch; var Cancel: WordBool);
        procedure OnAuthorized(ASender: TObject; const oSender: IDispatch;
        var Cancel: WordBool);
        procedure OnSendingDataStream(ASender: TObject; const oSender: IDispatch;
        Sent: Integer; Total: Integer;
        var Cancel: WordBool);
        procedure OnReceivingDataStream(ASender: TObject; const oSender: IDispatch;
        const oInfo: IDispatch;
        Received: Integer; Total: Integer;
        var Cancel: WordBool);
    private
        { Private declarations }
    public
        { Public declarations }
    end;

const
    MailServerPop3 = 0;
    MailServerImap4 = 1;
    MailServerEWS = 2;
    MailServerDAV = 3;

var
    Form1: TForm1;

implementation

{$R *.dfm}

// EAGetMail event handler
procedure TForm1.OnIdle(ASender: TObject; const oSender: IDispatch; var Cancel: WordBool);
begin
    Application.ProcessMessages();
end;

procedure TForm1.OnConnected(ASender: TObject; const oSender: IDispatch; var Cancel: WordBool);
begin
    Label1.Caption := 'Connected';
end;

procedure TForm1.OnQuit(ASender: TObject; const oSender: IDispatch; var Cancel: WordBool);
begin
    Label1.Caption := 'Disconnecting ...';
end;

procedure TForm1.OnSecuring(ASender: TObject; const oSender: IDispatch; var Cancel: WordBool);
begin
    Label1.Caption := 'Securing ...';
end;

procedure TForm1.OnAuthorized(ASender: TObject; const oSender: IDispatch;
    var Cancel: WordBool);
begin
    Label1.Caption := 'Authorized';
end;

procedure TForm1.OnSendingDataStream(ASender: TObject; const oSender: IDispatch;
    Sent: Integer; Total: Integer;
    var Cancel: WordBool);
begin
    //implementation code for IMAP4 appending event
end;

procedure TForm1.OnReceivingDataStream(ASender: TObject; const oSender: IDispatch;
    const oInfo: IDispatch;
    Received: Integer; Total: Integer;
    var Cancel: WordBool);
var
    info: IMailInfo;
begin
    info := oInfo as IMailInfo;
    Label1.Caption := Format( 'Receiving %d, %d/%d ...',
        [info.Index, Received, Total]);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
    oMail: IMail;
    oClient: TMailClient;
    oServer: TMailServer;
    oTools: TTools;
    infos: OleVariant;
    i, UBound, Count: integer;
    oInfo: IMailInfo;
    mailFolder: WideString;
    fileName: WideString;
begin
    try
        oTools := TTools.Create(Application);

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

        oServer := TMailServer.Create(Application);
        oServer.Server := 'pop3.emailarchitect.net';
        oServer.User := 'test@emailarchitect.net';
        oServer.Password := 'testpassword';
        oServer.Protocol := MailServerPop3;

        // If your server requires SSL connection
        // Please add the following codes
        // oServer.SSLConnection := true;
        // oServer.Port := 995;

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

        // Add eagetmail event handler
        // You don't have to use EAGetMail Event,
        // but using Event make your application more user friendly
        oClient.OnIdle := OnIdle;
        oClient.OnConnected := OnConnected;
        oClient.OnQuit := OnQuit;
        oClient.OnSecuring := OnSecuring;
        oClient.OnAuthorized := OnAuthorized;
        oClient.OnSendingDataStream := OnSendingDataStream;
        oClient.OnReceivingDataStream := OnReceivingDataStream;

        Label1.Caption := 'Connecting ...';
        oClient.Connect1(oServer.DefaultInterface);

        infos := oClient.GetMailInfos();
        UBound := VarArrayHighBound( infos, 1 );
        Count := UBound+1;
        ShowMessage(Format('Total %d email(s)', [Count]));

        for i := 0 to UBound do
        begin
            oInfo := IDispatch(VarArrayGet(infos, i)) as IMailInfo ;

            // Generate a random file name by current local datetime,
            // You can use your method to generate the filename if you do not like it
            fileName := mailFolder + '\' + oTools.GenFileName(i) + '.eml';

            // Receive email from POP3 server
            oMail := oClient.GetMail( oInfo );

            ShowMessage( 'From: ' + oMail.From.Address + #13#10 +
                'Subject: ' + oMail.Subject );

            // Save email to local disk
            oMail.SaveAs( fileName, true );

            // Mark email as deleted from POP3 server
            oClient.Delete(oInfo);
        end;

        // Quit and purge emails marked as deleted from POP3 server
        oClient.Quit;

    except
        on ep:Exception do
        ShowMessage( 'Error: ' + ep.Message );
    end;

    Label1.Caption := 'Completed!';
end;

end.

Next Section

At next section I will introduce how to use UIDL function to mark the email has been downloaded.

Appendix

Comments

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