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

Delphi ActiveX
var
success: Integer;
mail: TChilkatMailMan;
previews: TChilkatEmailBundle;
header: TChilkatEmail;
i: Integer;
email: TChilkatEmail;

begin
success := 0;

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

mail := TChilkatMailMan.Create(Self);
mail.MailHost := 'pop.example.com';
mail.MailPort := 995;
mail.PopSsl := 1;
mail.RequireSslCertVerify := 1;
mail.PopUsername := 'MAIL_USER';
mail.PopPassword := 'MAIL_PASSWORD';
previews := TChilkatEmailBundle.Create(Self);
//  Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
success := mail.FetchRange(1,1,0,0,4,previews.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(mail.LastErrorText);
    Exit;
  end;
if (previews.MessageCount = 0) then
  begin
    Memo1.Lines.Add('There are no messages to preview.');
    success := mail.Pop3EndSession();
    if (success <> 1) then
      begin
        Memo1.Lines.Add(mail.LastErrorText);
        Exit;
      end;
    Exit;
  end;
header := TChilkatEmail.Create(Self);
i := 0;
while (i < previews.MessageCount) do
  begin
    success := previews.EmailAt(i,header.ControlInterface);
    if (success <> 1) then
      begin
        Memo1.Lines.Add(previews.LastErrorText);
        Exit;
      end;
    Memo1.Lines.Add(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.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(previews.LastErrorText);
    Exit;
  end;
email := TChilkatEmail.Create(Self);
success := mail.FetchFull(header.ControlInterface,email.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(mail.LastErrorText);
    Exit;
  end;
success := mail.Pop3EndSession();
if (success <> 1) then
  begin
    Memo1.Lines.Add(mail.LastErrorText);
    Exit;
  end;
Memo1.Lines.Add('Full message: ' + email.Subject + '  Files: ' + IntToStr(email.NumAttachments));

success := email.SaveEml('pop-received.eml');
if (success <> 1) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;
Memo1.Lines.Add('Saved pop-received.eml.');