Delphi ActiveX
Delphi ActiveX
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 ActiveX Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
mailman: TChilkatMailMan;
seenUidlsPath: WideString;
sbXml: TChilkatStringBuilder;
htSeenUidls: TChilkatHashtable;
fac: TCkFileAccess;
stUidls: TChilkatStringTable;
stUnseenUidls: TChilkatStringTable;
uidl: WideString;
i: Integer;
count: Integer;
email: TChilkatEmail;
begin
success := 0;
// 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 := TChilkatMailMan.Create(Self);
// 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 := TChilkatStringBuilder.Create(Self);
htSeenUidls := TChilkatHashtable.Create(Self);
fac := TCkFileAccess.Create(Self);
if (fac.FileExists(seenUidlsPath) = 1) then
begin
success := sbXml.LoadFile(seenUidlsPath,'utf-8');
if (success = 0) then
begin
Memo1.Lines.Add(sbXml.LastErrorText);
Exit;
end;
htSeenUidls.AddFromXmlSb(sbXml.ControlInterface);
end;
// Get the complete list of UIDLs on the mail server.
stUidls := TChilkatStringTable.Create(Self);
success := mailman.FetchUidls(stUidls.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(mailman.LastErrorText);
Exit;
end;
// Build a list of unseen UIDLs
stUnseenUidls := TChilkatStringTable.Create(Self);
i := 0;
count := stUidls.Count;
while i < count do
begin
uidl := stUidls.StringAt(i);
if (htSeenUidls.Contains(uidl) <> 1) then
begin
stUnseenUidls.Append(uidl);
end;
i := i + 1;
end;
if (stUnseenUidls.Count = 0) then
begin
Memo1.Lines.Add('No unseen emails!');
Exit;
end;
// Download the unseen emails, adding each UIDL to the "seen" hash table.
email := TChilkatEmail.Create(Self);
count := stUnseenUidls.Count;
i := 0;
while i < count do
begin
// Download the full email.
uidl := stUnseenUidls.StringAt(i);
success := mailman.FetchByUidl(uidl,0,0,email.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(mailman.LastErrorText);
Exit;
end;
Memo1.Lines.Add(IntToStr(i));
Memo1.Lines.Add('From: ' + email.From);
Memo1.Lines.Add('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.ControlInterface);
success := sbXml.WriteFile(seenUidlsPath,'utf-8',0);
if (success = 0) then
begin
Memo1.Lines.Add(sbXml.LastErrorText);
Exit;
end;
Memo1.Lines.Add('Success.');
end;