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

Count the Related Items in an Email

See more Email Object Examples

Demonstrates the read-only Chilkat Email.NumRelatedItems property, which is the number of related MIME items in the email. Related items are resources — such as images or style sheets — embedded with an HTML body in a multipart/related enclosure, and are commonly referenced by the HTML using a cid: URL or a Content-Location. Indexes are zero-based. This example sets an HTML body, adds a related item, and prints the count.

Background: When an HTML email shows an image that travels inside the message (rather than being downloaded from the web), the image is a "related item." The HTML references it with <img src="cid:logo.png">, and a matching part carries a Content-ID of logo.png. The whole bundle lives in a multipart/related enclosure. This is different from an attachment: related items are meant to be displayed as part of the body, not saved as separate files.

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;
  cid: string;

begin
  //  Demonstrates the read-only Email.NumRelatedItems property.  Related items are
  //  resources (such as images or style sheets) embedded alongside an HTML body in a
  //  multipart/related enclosure, typically referenced from the HTML by a cid: URL.

  email := TEmail.Create;

  //  Set an HTML body that references a related item by content-id.
  email.SetHtmlBody('<html><body><img src="cid:logo.png"/></body></html>');

  //  Add the related item.  AddRelatedString returns the content-id assigned to it.
  //  The empty charset argument means no charset conversion is applied.
  cid := email.AddRelatedString('logo.png','(pretend this is image data)','');
  WriteLn('Related content-id = ' + cid);

  WriteLn('NumRelatedItems = ' + email.NumRelatedItems);


  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.