Sample code for 30+ languages & platforms
Delphi ActiveX

Get the Number of IMAP Attachments

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailNumAttach method, which returns the number of ordinary attachments on a message. The only argument is the Email. This example fetches only the headers and reports the attachment count.

Background: A header-only fetch downloads no attachment bodies, so the Email object's own downloaded-attachment count can be 0. GetMailNumAttach instead reads the ckx-imap-numAttach metadata, giving the true count — which is what you want when building a message list that shows whether each message has attachments.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
imap: TChilkatImap;
headersOnly: Integer;
useUid: Integer;
seqNum: Integer;
email: TChilkatEmail;
numAttach: Integer;

begin
success := 0;

//  Demonstrates the Imap.GetMailNumAttach method, which returns the number of ordinary
//  attachments on a message.  The only argument is the Email.

imap := TChilkatImap.Create(Self);

imap.Ssl := 1;
imap.Port := 993;

success := imap.Connect('imap.example.com');
if (success = 0) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;
success := imap.Login('user@example.com','myPassword');
if (success = 0) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;

success := imap.SelectMailbox('Inbox');
if (success = 0) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;

//  Fetch only the message headers.  Attachment bodies are NOT downloaded, but the ckx-imap-*
//  metadata describing the attachments is present, so the attachment info methods still work.
headersOnly := 1;
useUid := 0;
seqNum := 1;
email := TChilkatEmail.Create(Self);
success := imap.FetchEmail(headersOnly,seqNum,useUid,email.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;

numAttach := imap.GetMailNumAttach(email.ControlInterface);
Memo1.Lines.Add('This message has ' + IntToStr(numAttach) + ' attachment(s).');

success := imap.Disconnect();
if (success = 0) then
  begin
    Memo1.Lines.Add(imap.LastErrorText);
    Exit;
  end;