Using UIDL Function to Mark the Email has been downloaded/read in Delphi

In previous section, I introduced how to use event handler. In this section, I will introduce how to use UIDL function to mark the email has been downloaded in Delphi.

Introduction

If you want to leave a copy of email on the server, you should not call Delete method. However, there is a problem, how can you know if the email has already been downloaded? If there is a way to identify the downloaded email, you can avoid downloading the duplicated email from your POP3/IMAP4 server.

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 - IMAP4 Solution]

Every email has a unique identifier (UIDL) on IMAP4 server. It is a 32bit integer and it is always unique in your email account life time. So we can use the integer as file name to identify if the email has been downloaded. In order to run it correctly, please change email server, user, password, folder, file name values.

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;
        procedure Button1Click(Sender: TObject);
    private
        { Private declarations }
    public
        { Public declarations }
    end;

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

var
    Form1: TForm1;

implementation

{$R *.dfm}

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 := 'imap4.emailarchitect.net';
        oServer.User := 'test@emailarchitect.net';
        oServer.Password := 'testpassword';
        oServer.Protocol := MailServerImap4;

        // Set IMAP4 port
        oServer.Port := 143;

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

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

        oClient.Connect1(oServer.DefaultInterface);
        ShowMessage( 'Connected!' );

        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 ;
            ShowMessage(Format('Index: %d; Size: %d; UIDL: ' + oInfo.UIDL,
            [oInfo.Index, oInfo.Size]));

            // Using IMAP UIDL as the file name
            fileName := mailFolder + '\' + oInfo.UIDL + '.eml';

            if oTools.ExistFile(fileName) then
                continue; // this email has been download, do not receive it again.

            // Receive email from IMAP4 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 IMAP4 server
            oClient.Delete(oInfo);
        end;

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

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

end.

POP3/IMAP4/Exchange Web Service/WebDAV Solution

There is a little bit different in POP3 server. The UIDL is only unique in the email life time. That means if the email was deleted from the server, other email can use the old unique identifier. Another problem is: UIDL in POP3 server can be any number or characters, so we cannot use UIDL as the file name, because UIDL may contain invalid characters for file name.

UIDL is also unique in Exchange Web Service/WebDAV, but the UIDL is string but not integer. UIDL in Exchange server may contain invalid characters for file name, so we cannot use UIDL as the file name either.

To solve this problem, we have to store the UIDL to a txt file and synchronize it with server every time.

Please have a look at the following example code. It works with both POP3/IMAP4/Exchange Web Service/WebDAV protocol.

unit Unit1;

interface

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

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

        // UIDL functions
        procedure LoadUIDL();
        procedure UpdateUIDL();
        procedure SyncUIDL( oServer: TMailServer; infos: OleVariant);
        function FindExistedUIDL( oServer: TMailServer; UIDL: WideString ): Boolean;
        procedure AddUIDL( oServer: TMailServer; UIDL: WideString );

    private
        { Private declarations }
    public
        { Public declarations }
    end;

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

var
    Form1: TForm1;
    m_curpath: WideString;
    m_uidlfile: WideString;
    m_uidls: WideString;

implementation

{$R *.dfm}

procedure TForm1.LoadUIDL();
var
    oTools: TTools;
begin
    oTools := TTools.Create(Application);
    m_uidls := '';
    m_uidls := oTools.ReadTextFile(m_uidlfile, 0);
end;

procedure TForm1.UpdateUIDL();
var
    oTools: TTools;
begin
    try
        oTools := TTools.Create(Application);
        oTools.WriteTextFile( m_uidlfile, m_uidls, 0 );
    except
        on ep:Exception do
        ShowMessage( 'Update UIDL Error: ' + ep.Message );
    end;
end;

procedure TForm1.SyncUIDL( oServer: TMailServer; infos: OleVariant);
var
    index, i, UBound: integer;
    curpref, uidlpref, t, localuidl: WideString;
    uidls, newuidls: WideString;
    bRemove, bExistOnServer: boolean;
    oInfo: IMailInfo;
begin
    uidlpref := LowerCase(oServer.Server) + '#' +
        LowerCase(oServer.User) + ' ';

    uidls := m_uidls;
    UBound := VarArrayHighBound(infos, 1 );

    while true do
    begin
        index := Pos( #13#10, uidls );
        if( index <= 0 ) then
            break;

        t := LeftStr(uidls, index-1);
        uidls := MidStr( uidls, index + 2, Length(uidls)-index-2);
        bRemove := false;
        if Length(t) > Length(uidlpref) then
        begin
        curpref := LeftStr(t, Length(uidlpref));
        // this UIDL belongs to current server/user
        if CompareStr(curpref, uidlpref) = 0 then
        begin
            localuidl := MidStr( t, Length(curpref)+1, Length(t)-Length(curpref));
            bExistOnServer := false;
            for i := 0 to UBound do
            begin
            oInfo := IDispatch(VarArrayGet(infos, i)) as IMailInfo;
            if CompareStr(oInfo.UIDL, localuidl ) = 0 then
                begin
                bExistOnServer := true;
                break;
                end;
            end;

            if not bExistOnServer then
            bRemove := true;

            if trim(t) = '' then
            bRemove := true;
        end;
        end;

        if not bRemove then
        newuidls := newuidls + t + #13#10;
    end;

    m_uidls := newuidls;
end;

function TForm1.FindExistedUIDL( oServer: TMailServer; UIDL: WideString ): Boolean;
var
    s: WideString;
    index: integer;
begin
    s := LowerCase(oServer.Server) + '#' +
        LowerCase(oServer.User) + ' ' + UIDL + #13#10;

    index := Pos( s, m_uidls );
    if index > 0 then
        FindExistedUIDL := true
    else
        FindExistedUIDL := false;
end;

procedure TForm1.AddUIDL( oServer: TMailServer; UIDL: WideString );
begin
    m_uidls :=  m_uidls + LowerCase(oServer.Server) + '#' +
        LowerCase(oServer.User) + ' ' + UIDL + #13#10;
end;

function TForm1.SearchFolder(folders: OleVariant; name: WideString)
:IImap4Folder;
var
    i, UBound: integer;
    folder: IImap4Folder;
begin
    UBound := VarArrayHighBound(folders, 1 );
    for i:= 0 to UBound do
    begin
        folder := IDispatch(VarArrayGet(folders,i)) as IImap4Folder;
        if CompareText( folder.Name, name ) = 0 then
        begin
        Result := folder;
        exit;
        end;

        // Search folder in sub-folders
        folder := SearchFolder( folder.SubFolders, name );
        if not (folder = nil ) then
        begin
            Result := folder;
            exit;
        end;
    end;
    // No folder found
    Result := nil;
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;
    leaveCopy: Boolean;
begin
    try
        oTools := TTools.Create(Application);

        // Create a folder named "inbox" under
        // current directory to store the email files
        mailFolder := GetCurrentDir() + '\inbox';
        m_curpath := GetCurrentDir();
        m_uidlfile := m_curpath + '\uidl.txt';
        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;

        // leave a copy of message on server
        leaveCopy := true;
        oClient := TMailClient.Create(Application);
        oClient.LicenseCode := 'TryIt';

        oClient.Connect1(oServer.DefaultInterface);
        ShowMessage( 'Connected!' );

        // UIDL is the identifier of every email on POP3/IMAP4 server, to avoid retrieve
        // the same email from server more than once, we record the email uidl retrieved every time
        // if you delete the email from server every time and not to leave a copy of email on
        // the server, then please remove all the function about uidl.
        LoadUIDL();

        infos := oClient.GetMailInfos();
        UBound := VarArrayHighBound( infos, 1 );
        Count := UBound+1;

        SyncUIDL( oServer, infos );

        ShowMessage(Format('Total %d email(s)', [Count]));

        for i := 0 to UBound do
        begin
            oInfo := IDispatch(VarArrayGet(infos, i)) as IMailInfo ;
            ShowMessage(Format('Index: %d; Size: %d; UIDL: ' + oInfo.UIDL,
            [oInfo.Index, oInfo.Size]));

            if FindExistedUIDL(oServer, oInfo.UIDL) then
                continue; // this email has been downloaded, do not receive it again

            // 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 );

            if leaveCopy Then
            begin
                //add the email uidl to uidl file to avoid we retrieve it next time.
                AddUIDL( oServer, oInfo.UIDL );
            end
            else
            begin
                // Mark email as deleted from IMAP4 server
                oClient.Delete(oInfo);
            end;
        end;

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

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

    UpdateUIDL();

end;

end.

UIDLManager Object

With EAGetMail 4.0, it provides a new class named “UIDLManager”. This object provides an easy way to maintain UIDL between your server and your local client. How does it work? It stores UIDL collection to a local disk file and you can use this object to add, remove and search UIDL with this local file. Then you don’t have to handle UIDL in your code. Please click here to learn more detail.

Mark Email as Read on IMAP4/Exchange Server

With IMAP4/Exchange Web Service/WebDAV protocol, you can also mark the email as read on the server, but POP3 doesn’t support this feature. Please refer to MarkAsRead method to learn more detail.

Next Section

At next section I will introduce how to download email in background.

Appendix

Comments

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