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

Get Each Alternative Body of an Email

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeBody method, which returns the Nth alternative body. The NumAlternatives property gives the number of alternatives, and indexes are zero-based. This example builds a message with plain-text and HTML alternatives and prints each one.

Background: A multipart/alternative email carries the same content in several forms — commonly plain text and HTML. Enumerating them by index lets you pull out exactly the representation you need: showing the HTML in a rich viewer, extracting the plain text for search or indexing, or comparing the two. Pair this with GetAlternativeContentType to learn what each indexed body actually is.

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;
  n: Integer;
  i: Integer;

begin
  //  Demonstrates the GetAlternativeBody method, which returns the Nth alternative body.
  //  The NumAlternatives property gives the number of alternatives; indexes are zero-based.

  email := TEmail.Create;

  //  Create an email with plain-text and HTML alternatives.
  email.SetTextBody('This is the plain-text alternative.','text/plain');
  email.AddHtmlAlternativeBody('<html><body>This is the HTML alternative.</body></html>');

  n := email.NumAlternatives;

  for i := 0 to n - 1 do
    begin
      WriteLn('---- Alternative ' + i + ' ----');
      WriteLn(email.GetAlternativeBody(i));
    end;



  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.