Sample code for 30+ languages & platforms
Delphi DLL 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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
mail: HCkMailMan;
previews: HCkEmailBundle;
header: HCkEmail;
i: Integer;
email: HCkEmail;

begin
success := False;

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

mail := CkMailMan_Create();
CkMailMan_putMailHost(mail,'pop.example.com');
CkMailMan_putMailPort(mail,995);
CkMailMan_putPopSsl(mail,True);
CkMailMan_putRequireSslCertVerify(mail,True);
CkMailMan_putPopUsername(mail,'MAIL_USER');
CkMailMan_putPopPassword(mail,'MAIL_PASSWORD');
previews := CkEmailBundle_Create();
//  Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
success := CkMailMan_FetchRange(mail,True,True,0,0,4,previews);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mail));
    Exit;
  end;
if (CkEmailBundle_getMessageCount(previews) = 0) then
  begin
    Memo1.Lines.Add('There are no messages to preview.');
    success := CkMailMan_Pop3EndSession(mail);
    if (success <> True) then
      begin
        Memo1.Lines.Add(CkMailMan__lastErrorText(mail));
        Exit;
      end;
    Exit;
  end;
header := CkEmail_Create();
i := 0;
while (i < CkEmailBundle_getMessageCount(previews)) do
  begin
    success := CkEmailBundle_EmailAt(previews,i,header);
    if (success <> True) then
      begin
        Memo1.Lines.Add(CkEmailBundle__lastErrorText(previews));
        Exit;
      end;
    Memo1.Lines.Add(CkEmail__uidl(header) + '  ' + CkEmail__subject(header));
    i := i + 1;
  end;

//  A UI could use its selected row here. FetchFull uses the preview's POP3 identity.
success := CkEmailBundle_EmailAt(previews,0,header);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmailBundle__lastErrorText(previews));
    Exit;
  end;
email := CkEmail_Create();
success := CkMailMan_FetchFull(mail,header,email);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mail));
    Exit;
  end;
success := CkMailMan_Pop3EndSession(mail);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mail));
    Exit;
  end;
Memo1.Lines.Add('Full message: ' + CkEmail__subject(email) + '  Files: ' + IntToStr(CkEmail_getNumAttachments(email)));

success := CkEmail_SaveEml(email,'pop-received.eml');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
Memo1.Lines.Add('Saved pop-received.eml.');

CkMailMan_Dispose(mail);
CkEmailBundle_Dispose(previews);
CkEmail_Dispose(header);
CkEmail_Dispose(email);