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

Set the Charset of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.SetAttachmentCharset method, which sets the charset parameter of the Content-Type header field for the attachment at a given zero-based index. This example adds a text attachment and sets its charset to utf-8.

Background: For a text attachment, the charset parameter tells the receiving client which character encoding the bytes use, so accented or non-Latin text renders correctly. Setting it explicitly (typically utf-8) removes ambiguity when the attachment contains non-ASCII content — without it, a client may guess wrong and display garbled characters.

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 SetAttachmentCharset method, which sets the charset parameter of the
  //  Content-Type header field for the attachment at the given zero-based index.

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

  email.AddStringAttachment('notes.txt','Some notes.');

  //  Set the charset of the first attachment (index 0) to utf-8.
  success := email.SetAttachmentCharset(0,'utf-8');
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  //  The attachment's Content-Type now includes charset="utf-8".
  WriteLn(email.GetMime());


  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.