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

B-Encode a String for MIME Headers

See more Email Object Examples

Demonstrates the Chilkat Email.BEncodeString method, which converts the Unicode string in the first argument to the charset named in the second argument, B-encodes the resulting multibyte data, and returns the encoded string. This is the representation used for non-ASCII text in MIME header fields.

Background: MIME headers were defined to carry only plain ASCII, so non-ASCII text (accents, non-Latin scripts) must be wrapped in an "encoded-word" per RFC 2047. The B encoding is Base64-based: the text becomes something like =?utf-8?B?...?=, which mail software recognizes and decodes back to the original characters. There is also a Q (quoted-printable-style) encoding for the same purpose; B-encoding is preferred when most characters are non-ASCII.

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;
  encoded: string;

begin
  //  Demonstrates the BEncodeString method, which converts a Unicode string to a specified
  //  charset, B-encodes (RFC 2047) the resulting bytes, and returns the encoded string.
  //  This is the form used for non-ASCII text in MIME header fields.

  email := TEmail.Create;

  //  B-encode a Unicode string using the utf-8 charset.
  encoded := email.BEncodeString('Cafe Meeting Notes','utf-8');

  WriteLn('B-encoded: ' + encoded);


  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.