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

Strip Path Info from Attachment Filenames

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveAttachmentPaths method, which removes relative or absolute path information from the attachment filenames stored in the MIME, leaving only each final filename component. This example adds an attachment whose name includes a path and shows the filename before and after.

Background: An attachment filename that carries a path (like reports/2026/q3/summary.txt) is a problem: some mail clients display the whole path, and — more seriously — a path with .. segments can be a directory-traversal risk when attachments are saved to disk. Reducing every filename to just its final component (summary.txt) makes the message cleaner and safer to handle.

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;

begin
  //  Demonstrates the RemoveAttachmentPaths method, which removes relative or absolute path
  //  information from attachment filenames stored in the MIME, leaving only each final
  //  filename component.

  email := TEmail.Create;
  email.Subject := 'Remove attachment paths';

  //  Add an attachment whose filename includes path information.
  email.AddStringAttachment('reports/2026/q3/summary.txt','Attachment content.');
  WriteLn('Filename before: ' + email.GetAttachmentFilename(0));

  //  Strip the path, leaving only the final filename component.
  email.RemoveAttachmentPaths();
  WriteLn('Filename after: ' + email.GetAttachmentFilename(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.