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

Change the Filename of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.SetAttachmentFilename method, which changes the filename of the attachment at a given zero-based index. This example adds an attachment and renames it, printing the filename before and after.

Background: The attachment filename is what a recipient sees and what most clients suggest when saving the file. Renaming it is handy when the original name is unclear, unsafe, or generic — for example giving a machine-generated tmp12345 a meaningful name like invoice.pdf before sending.

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 SetAttachmentFilename method, which changes the filename of the
  //  attachment at the given zero-based index.

  email := TEmail.Create;
  email.Subject := 'Set attachment filename';

  email.AddStringAttachment('oldname.txt','Some notes.');
  WriteLn('Filename before: ' + email.GetAttachmentFilename(0));

  //  Change the filename of the first attachment (index 0).
  success := email.SetAttachmentFilename(0,'newname.txt');
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  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.