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

Get the Filename of a Related Item

See more Email Object Examples

Demonstrates the Chilkat Email.GetRelatedFilename method, which returns the filename of a related item contained within the email. Related items are typically images and style sheets embedded within HTML emails, and the index is zero-based. This example adds a related style sheet and reads its filename.

Background: A related item's filename is how the HTML body refers to it — for example a style sheet named styles.css or an image named logo.png. When unpacking or archiving an HTML email, this filename is the natural name to write each embedded resource to on disk so the rewritten HTML links continue to resolve.

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

begin
  //  Demonstrates the GetRelatedFilename method, which returns the filename of a related item
  //  contained within the email.  Related items are typically images and style sheets
  //  embedded within HTML emails.  The index is zero-based.

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

  //  The HTML references a related style sheet by name (Content-Location).
  email.SetHtmlBody('<html><head><link rel="stylesheet" href="styles.css"/></head><body>Styled.</body></html>');

  //  Add the related style sheet (index 0).
  email.AddRelatedString2('styles.css','body { color: navy; }','utf-8');

  //  Read the filename of the first related item (index 0).
  fname := email.GetRelatedFilename(0);
  WriteLn('Related item 0 filename: ' + fname);


  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.