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

Email Body - Plain Text and/or HTML

Any given email may have a plain-text body, an HTML body, or both. The Body property will return the HTML body by default (if it exists) otherwise it will return the plain-text body. There are methods for checking to see if an email has a particular body (HasPlainTextBody and HasHtmlBody) and there are methods for getting a specific body: GetHtmlBody, GetPlainTextBody.

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;
  bText: Boolean;
  bHtml: Boolean;

begin
  success := False;

  email := TEmail.Create;

  //  Load an email from a .eml

  success := email.LoadEml('something.eml');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  //  Display the default Body:
  WriteLn(email.Body);

  //  If a plain-text body is present, display it:

  bText := email.HasPlainTextBody();
  if (bText = True) then
    begin
      WriteLn(email.GetPlainTextBody());
    end;

  //  If an HTML body is present, display the HTML source:

  bHtml := email.HasHtmlBody();
  if (bHtml = True) then
    begin
      WriteLn(email.GetHtmlBody());
    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.