Sample code for 30+ languages & platforms
Delphi DLL

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

Delphi DLL
var
success: Boolean;
imap: HCkImap;
headersOnly: Boolean;
useUid: Boolean;
seqNum: Integer;
email: HCkEmail;
numAttach: Integer;

begin
success := False;

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

imap := CkImap_Create();

CkImap_putSsl(imap,True);
CkImap_putPort(imap,993);

success := CkImap_Connect(imap,'imap.example.com');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;
success := CkImap_Login(imap,'user@example.com','myPassword');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

success := CkImap_SelectMailbox(imap,'Inbox');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    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 := True;
useUid := False;
seqNum := 1;
email := CkEmail_Create();
success := CkImap_FetchEmail(imap,headersOnly,seqNum,useUid,email);
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

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

success := CkImap_Disconnect(imap);
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

CkImap_Dispose(imap);
CkEmail_Dispose(email);