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

Change the Filename of a Related Item

See more Email Object Examples

Demonstrates the Chilkat Email.SetRelatedFilename method, which sets (changes) the filename stored for a related item within the email. The index is zero-based. This example adds a related style sheet and renames it, printing the filename before and after.

Background: A related item's filename identifies the embedded resource within the message. Changing it is occasionally needed to match how the HTML body references the resource, or to give an embedded image or style sheet a cleaner name. Keep in mind that if the HTML links to the item by that name (via Content-Location), the reference should be kept in sync with the new filename.

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
  success: Boolean;
  email: TEmail;

begin
  success := False;

  //  Demonstrates the SetRelatedFilename method, which sets (changes) the filename of a
  //  related item within the email.  The index is zero-based.

  email := TEmail.Create;
  email.Subject := 'Set related item filename';

  //  Set an HTML body and add a related style sheet (index 0).
  email.SetHtmlBody('<html><head><link rel="stylesheet" href="styles.css"/></head><body>Styled.</body></html>');
  email.AddRelatedString2('styles.css','body { color: navy; }','utf-8');
  WriteLn('Related filename before: ' + email.GetRelatedFilename(0));

  //  Change the filename of the related item at index 0.
  success := email.SetRelatedFilename(0,'main.css');
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  WriteLn('Related filename after: ' + email.GetRelatedFilename(0));


  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.