EAGetMail POP3 & IMAP4 Component > Developer Center > Retrieve Email and Parse Email in Delphi - Tutorial

Retrieve Email and Parse Email in Delphi - Tutorial

Introduction

EAGetMail is a POP3 & IMAP4 component which supports all operations of POP3/IMAP4/MIME protocol. This tutorial covers everything of retrieving email and parsing email with EAGetMail in Delphi.

Installation

Before you can use the following sample codes, you should download the EAGetMail Installer and install it on your machine at first.

A simple Delphi project

To better demonstrate how to use EAGetMail retrieving and parsing email, let's create a Delphi Standard EXE project at first, then add a TButton on the Form, double-click this button. It is like this:

Delphi form project

Add Reference of EAGetMail to Delphi Project

To use EAGetMail POP3 & IMAP4 ActiveX Object in your project, the first step is "Add reference of EAGetMail to your project". Please choose menu->"Project" -> "Import Type Library"- > and choose the "EAGetMailObj ActiveX Object", click "Create Unit", the reference of EAGetMail ActiveX Object will be added to your project, and you can start to use EAGetMail to retrieve email and parse email in your project.

add reference in Delphi

[Delphi Example - Retrieve email from POP3 server]

Now add the following codes to the project. The following code demonstrates how to retrieve email from a POP3 mail account. This sample downloads emails from POP3 server and deletes the email after the email is retrieved.

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;

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 := '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';

    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 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 pure emails marked as deleted from POP3 server
    oClient.Quit;

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

end.

If you set everything right, you can get emails in the mail folder. If the codes threw exception, then please have a look at the following section.

Where can I get my POP3 server address, user and password?

Because each email account provider has different server address, so you should query your POP3 server address from your email account provider. User name is your email address or your email address without domain part. It depends on your email provider setting.

When you execute above example code, if you get error about "Networking connection" or "No such host", it is likely that your POP3 server address is not correct. If you get an error like "Invalid user or password", it is likely that you did not set the correct user or password.

Finally, if you have already set your account in your email client such as Outlook or Window Mail, you can query your POP3 server address, user in your email client. For example, you can choose menu -> "Tools" - > - "Accounts" - > "Your email account" - > "Properties" - > "Servers" in Outlook express or Windows Mail to get your POP3 server, user. Using EAGetMail to receive email does not require you have email client installed on your machine or MAPI, however you can query your exist email accounts in your email client.

Delphi console email sample

Next Section

In this section, I introduced the basic things of retrieving email in Delphi with EAGetMail. At next section I will introduce how to retrieve email from IMAP4 server.

Next: Retrieve email from IMAP4 server in Delphi ->

Comments

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

Download

Send Email - C# - VB6 - Visual Basic - VC++ - Managed C++ - Delphi

Retrieve Email and Parse Email - C# - VB6 - Visual Basic - VC++ - Managed C++ - Delphi

Email Solution - Email Server - DomainKeys/DKIM - Disclaimer

2003 - 2011 © Copyright AdminSystem Software Limited. All rights reserved.   About us     Follow emailarchitect on Twitter