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

Get and Set the Email Body

See more Email Object Examples

Demonstrates the Chilkat Email.Body property. When you set Body, Chilkat inspects the content and decides whether it should be treated as HTML or plain-text. When you read Body, an email that contains both plain-text and HTML alternatives returns the HTML body — use GetHtmlBody or GetPlainTextBody to retrieve a specific representation. This example sets a plain-text body and reads it back.

Background: A single email often carries two versions of the same message — a plain-text body and an HTML body — bundled together in a multipart/alternative MIME structure. The receiving mail client picks whichever version it prefers to display. Because of this, "the body" is ambiguous: the Body property favors the HTML version when both are present, which is why Chilkat also provides GetHtmlBody, GetPlainTextBody, HasHtmlBody, and HasPlainTextBody for precise control.

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

begin
  //  Demonstrates getting and setting the Email.Body property.
  //  When setting Body, Chilkat inspects the content to decide whether it is
  //  HTML or plain-text.  When reading, an email that has both plain-text and
  //  HTML alternatives returns the HTML body.

  email := TEmail.Create;

  //  Set the body.
  email.Body := 'This is the plain-text body of the email.';

  //  Read the Body property back.
  bodyText := email.Body;
  WriteLn('Body = ' + bodyText);


  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.