Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Preview POP3 messages and download a complete email

See more Email Object Examples

Read a short preview for each of up to five messages into an EmailBundle, print their subjects, and download the complete first entry into a separate Email. Configure a POP3 account that supports UIDL and TOP. All messages stay on the server.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.MailMan,
  Chilkat.EmailBundle,
  Chilkat.Email;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  mail: TMailMan;
  previews: TEmailBundle;
  header: TEmail;
  i: Integer;
  email: TEmail;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.

  mail := TMailMan.Create;
  mail.MailHost := 'pop.example.com';
  mail.MailPort := 995;
  mail.PopSsl := True;
  mail.RequireSslCertVerify := True;
  mail.PopUsername := 'MAIL_USER';
  mail.PopPassword := 'MAIL_PASSWORD';
  previews := TEmailBundle.Create;
  //  Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
  success := mail.FetchRange(True,True,0,0,4,previews);
  if (success <> True) then
    begin
      WriteLn(mail.LastErrorText);
      Exit;
    end;
  if (previews.MessageCount = 0) then
    begin
      WriteLn('There are no messages to preview.');
      success := mail.Pop3EndSession();
      if (success <> True) then
        begin
          WriteLn(mail.LastErrorText);
          Exit;
        end;
      Exit;
    end;
  header := TEmail.Create;
  i := 0;
  while (i < previews.MessageCount) do
    begin
      success := previews.EmailAt(i,header);
      if (success <> True) then
        begin
          WriteLn(previews.LastErrorText);
          Exit;
        end;
      WriteLn(header.Uidl + '  ' + header.Subject);
      i := i + 1;
    end;

  //  A UI could use its selected row here. FetchFull uses the preview's POP3 identity.
  success := previews.EmailAt(0,header);
  if (success <> True) then
    begin
      WriteLn(previews.LastErrorText);
      Exit;
    end;
  email := TEmail.Create;
  success := mail.FetchFull(header,email);
  if (success <> True) then
    begin
      WriteLn(mail.LastErrorText);
      Exit;
    end;
  success := mail.Pop3EndSession();
  if (success <> True) then
    begin
      WriteLn(mail.LastErrorText);
      Exit;
    end;
  WriteLn('Full message: ' + email.Subject + '  Files: ' + email.NumAttachments);

  success := email.SaveEml('pop-received.eml');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  WriteLn('Saved pop-received.eml.');


  mail.Free;
  previews.Free;
  header.Free;
  email.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.