Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Count the MIME Parts of a Given Content-Type

See more Email Object Examples

Demonstrates the Chilkat Email.GetNumPartsOfType method, which returns the number of non-multipart MIME parts matching a Content-Type pattern. The first argument is the content-type pattern (an exact type or a wildcard), the second is an inlineOnly flag, and the third is an excludeAttachments flag. This example counts the text/* parts.

Background: This is the companion of GetNthTextPartOfType: it tells you how many parts match a pattern so you can loop over them by index. Wildcards like text/* or image/* make it easy to ask questions such as "how many images does this message contain?" while the boolean flags let you include or exclude attachment parts from the count.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Email;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  email: TEmail;
  n: Integer;

begin
  //  Demonstrates the GetNumPartsOfType method, which returns the number of non-multipart
  //  MIME parts matching a Content-Type pattern.  The first argument is the Content-Type
  //  pattern (exact or wildcard), the second is inlineOnly, and the third is excludeAttachments.

  email := TEmail.Create;
  email.Subject := 'GetNumPartsOfType example';

  email.SetTextBody('This is the plain-text version.','text/plain');
  email.AddHtmlAlternativeBody('<html><body>This is the HTML version.</body></html>');

  //  Count the non-multipart parts whose content type matches text/*.
  n := email.GetNumPartsOfType('text/*',False,False);
  WriteLn('Number of text/* parts = ' + n);


  email.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.