Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Set the Charset of an Email
See more Email Object Examples
Demonstrates the Chilkat Email.Charset property, which represents the main charset of the email, such as utf-8, iso-8859-1, or Shift_JIS. Chilkat stores text internally as Unicode; this property mainly controls how the body text is converted to bytes and labeled when MIME is generated. This example sets the charset to utf-8 and prints the resulting MIME.
Background: Email is transmitted as bytes, but text can contain characters from many languages. A charset (character encoding) is the rule that maps characters to bytes. The email's MIME declares its charset in the
Content-Type header (e.g. text/plain; charset="utf-8") so the receiving client can correctly turn the bytes back into readable text. utf-8 is the modern default because it can represent virtually every character; using the wrong charset is what produces "mojibake" (garbled characters).Chilkat Pascal (Lazarus/Delphi) Downloads
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 Email.Charset property, which controls the main charset
// used when the email's MIME is generated (e.g. utf-8, iso-8859-1, Shift_JIS).
email := TEmail.Create;
email.Subject := 'Charset example';
email.Body := 'This email body will be labeled with the utf-8 charset.';
// Set the main charset.
email.Charset := 'utf-8';
WriteLn('Charset = ' + email.Charset);
// The generated MIME labels the body with the utf-8 charset.
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.