Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Reading Unread POP3 Email

The POP3 protocol cannot determine which emails are "unread," and pure POP3 servers do not store this information. Servers like Exchange Server, offering both POP3 and IMAP interfaces, do contain read/unread data, but it's only accessible through IMAP. Email clients like Outlook and Thunderbird store read/unread statuses on the client side. The example demonstrates using UIDLs to track and manage "unread" emails.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.StringTable,
  Chilkat.Hashtable,
  Chilkat.Email,
  Chilkat.FileAccess,
  Chilkat.StringBuilder,
  Chilkat.MailMan;

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

procedure RunDemo;
var
  success: Boolean;
  mailman: TMailMan;
  seenUidlsPath: string;
  sbXml: TStringBuilder;
  htSeenUidls: THashtable;
  fac: TFileAccess;
  stUidls: TStringTable;
  stUnseenUidls: TStringTable;
  uidl: string;
  i: Integer;
  count: Integer;
  email: TEmail;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  //  The mailman object is used for receiving (POP3) 
  //  and sending (SMTP) email.
  mailman := TMailMan.Create;

  //  Set the POP3 server's hostname
  mailman.MailHost := 'pop.example.com';

  //  Set the POP3 login/password.
  mailman.PopUsername := '***';
  mailman.PopPassword := '***';

  //  Keep a records of already-seen UIDLs in hash table serialized to an XML file.
  seenUidlsPath := 'c:/temp/seenUidls.xml';

  sbXml := TStringBuilder.Create;
  htSeenUidls := THashtable.Create;
  fac := TFileAccess.Create;
  if (fac.FileExists(seenUidlsPath) = True) then
    begin
      success := sbXml.LoadFile(seenUidlsPath,'utf-8');
      if (success = False) then
        begin
          WriteLn(sbXml.LastErrorText);
          Exit;
        end;
      htSeenUidls.AddFromXmlSb(sbXml);
    end;

  //  Get the complete list of UIDLs on the mail server.
  stUidls := TStringTable.Create;
  success := mailman.FetchUidls(stUidls);
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  //  Build a list of unseen UIDLs
  stUnseenUidls := TStringTable.Create;

  i := 0;
  count := stUidls.Count;
  while i < count do
    begin
      uidl := stUidls.StringAt(i);
      if (htSeenUidls.Contains(uidl) <> True) then
        begin
          stUnseenUidls.Append(uidl);
        end;
      i := i + 1;
    end;

  if (stUnseenUidls.Count = 0) then
    begin
      WriteLn('No unseen emails!');
      Exit;
    end;

  //  Download the unseen emails, adding each UIDL to the "seen" hash table.
  email := TEmail.Create;

  count := stUnseenUidls.Count;
  i := 0;
  while i < count do
    begin
      //  Download the full email.
      uidl := stUnseenUidls.StringAt(i);
      success := mailman.FetchByUidl(uidl,False,0,email);
      if (success = False) then
        begin
          WriteLn(mailman.LastErrorText);
          Exit;
        end;

      WriteLn(i);
      WriteLn('From: ' + email.From);
      WriteLn('Subject: ' + email.Subject);

      //  Add this UIDL to the "seen" hash table.
      htSeenUidls.AddStr(uidl,'');

      i := i + 1;
    end;

  mailman.Pop3EndSession();

  //  Update the "seen" UIDLs file.
  sbXml.Clear();
  htSeenUidls.ToXmlSb(sbXml);
  success := sbXml.WriteFile(seenUidlsPath,'utf-8',False);
  if (success = False) then
    begin
      WriteLn(sbXml.LastErrorText);
      Exit;
    end;

  WriteLn('Success.');


  mailman.Free;
  sbXml.Free;
  htSeenUidls.Free;
  fac.Free;
  stUidls.Free;
  stUnseenUidls.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.