Download/Retrieve Email from Yahoo account in Delphi

In previous section, I introduced how to download emails from Gmail account. In this section, I will introduce how to download emails from your Yahoo account in Delphi.

Introduction

Yahoo POP3 server address is pop.mail.yahoo.com. It requires SSL connection on 995 port; Yahoo IMAP4 server address is imap.mail.yahoo.com . It requires SSL connection on 993 port. You should use your Yahoo email address as the user name for user authentication. For example: your email is myid@yahoo.com, and then the user name should be myid@yahoo.com.

Server Port SSL Protocol
pop.mail.yahoo.com 995 SSL required Pop3
imap.mail.yahoo.com 993 SSL required Imap4

Important

If you got authentication error, you need to enable Allowing less secure apps in your Yahoo account. Although Yahoo supports OAUTH, but it doesn’t provide mail permission, so OAUTH is not a solution for Yahoo mail.

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 emails from Yahoo account using POP3]

The following example codes demonstrate how to download emails from Yahoo account using POP3 protocol.

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

        // Yahoo POP3 server address
        oServer.Server := 'pop.mail.yahoo.com';
        oServer.User := 'yahooid@yahoo.com';
        oServer.Password := 'testpassword';
        oServer.Protocol := MailServerPop3;

        // Enable SSL connection
        oServer.SSLConnection := true;

        // Set 995 SSL Port
        oServer.Port := 995;

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

            // 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 Yahoo 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 Yahoo POP3 server
            oClient.Delete(oInfo);
        end;

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

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

end.

[Delphi Example - Retrieve emails from Yahoo account using IMAP4]

The following example codes demonstrate how to download emails from Yahoo account using IMAP4 protocol.

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

        // Yahoo IMAP4 server address is "imap.mail.yahoo.com"
        oServer.Server := 'imap.mail.yahoo.com';
        oServer.User := 'yahooid@yahoo.com';
        oServer.Password := 'testpassword';
        oServer.Protocol := MailServerImap4;

        // Enable SSL connection
        oServer.SSLConnection := true;

        // Set 993 IMAP4 SSL Port
        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]));

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

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

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

end.

Next Section

At next section I will introduce how to download emails from Hotmail/MSN Live/Office 365 account.

Appendix

Comments

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