Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringTable, Hashtable, Email, FileAccess, StringBuilder, MailMan;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
mailman: HCkMailMan;
seenUidlsPath: PWideChar;
sbXml: HCkStringBuilder;
htSeenUidls: HCkHashtable;
fac: HCkFileAccess;
stUidls: HCkStringTable;
stUnseenUidls: HCkStringTable;
uidl: PWideChar;
i: Integer;
count: Integer;
email: HCkEmail;

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 := CkMailMan_Create();

// Set the POP3 server's hostname
CkMailMan_putMailHost(mailman,'pop.example.com');

// Set the POP3 login/password.
CkMailMan_putPopUsername(mailman,'***');
CkMailMan_putPopPassword(mailman,'***');

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

sbXml := CkStringBuilder_Create();
htSeenUidls := CkHashtable_Create();
fac := CkFileAccess_Create();
if (CkFileAccess_FileExists(fac,seenUidlsPath) = True) then
  begin
    success := CkStringBuilder_LoadFile(sbXml,seenUidlsPath,'utf-8');
    if (success = False) then
      begin
        Memo1.Lines.Add(CkStringBuilder__lastErrorText(sbXml));
        Exit;
      end;
    CkHashtable_AddFromXmlSb(htSeenUidls,sbXml);
  end;

// Get the complete list of UIDLs on the mail server.
stUidls := CkStringTable_Create();
success := CkMailMan_FetchUidls(mailman,stUidls);
if (success = False) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

// Build a list of unseen UIDLs
stUnseenUidls := CkStringTable_Create();

i := 0;
count := CkStringTable_getCount(stUidls);
while i < count do
  begin
    uidl := CkStringTable__stringAt(stUidls,i);
    if (CkHashtable_Contains(htSeenUidls,uidl) <> True) then
      begin
        CkStringTable_Append(stUnseenUidls,uidl);
      end;
    i := i + 1;
  end;

if (CkStringTable_getCount(stUnseenUidls) = 0) then
  begin
    Memo1.Lines.Add('No unseen emails!');
    Exit;
  end;

// Download the unseen emails, adding each UIDL to the "seen" hash table.
email := CkEmail_Create();

count := CkStringTable_getCount(stUnseenUidls);
i := 0;
while i < count do
  begin
    // Download the full email.
    uidl := CkStringTable__stringAt(stUnseenUidls,i);
    success := CkMailMan_FetchByUidl(mailman,uidl,False,0,email);
    if (success = False) then
      begin
        Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
        Exit;
      end;

    Memo1.Lines.Add(IntToStr(i));
    Memo1.Lines.Add('From: ' + CkEmail__from(email));
    Memo1.Lines.Add('Subject: ' + CkEmail__subject(email));

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

    i := i + 1;
  end;

CkMailMan_Pop3EndSession(mailman);

// Update the "seen" UIDLs file.
CkStringBuilder_Clear(sbXml);
CkHashtable_ToXmlSb(htSeenUidls,sbXml);
success := CkStringBuilder_WriteFile(sbXml,seenUidlsPath,'utf-8',False);
if (success = False) then
  begin
    Memo1.Lines.Add(CkStringBuilder__lastErrorText(sbXml));
    Exit;
  end;

Memo1.Lines.Add('Success.');

CkMailMan_Dispose(mailman);
CkStringBuilder_Dispose(sbXml);
CkHashtable_Dispose(htSeenUidls);
CkFileAccess_Dispose(fac);
CkStringTable_Dispose(stUidls);
CkStringTable_Dispose(stUnseenUidls);
CkEmail_Dispose(email);

end;