Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Read email headers and display the body as text

See more Email Object Examples

Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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,
  Chilkat.HtmlToText;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  email: TEmail;
  i: Integer;
  referenceId: string;
  reader: THtmlToText;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.

  email := TEmail.Create;
  success := email.LoadEml('with-attachments.eml');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  WriteLn('From: ' + email.From);
  WriteLn('Subject: ' + email.Subject);
  WriteLn('Date: ' + email.EmailDateStr);
  i := 0;
  while (i < email.NumTo) do
    begin
      WriteLn('To: ' + email.GetToName(i) + '  ' + email.GetToAddr(i));
      i := i + 1;
    end;

  referenceId := email.GetHeaderField('X-Reference-ID');
  if (email.LastMethodSuccess = True) then
    begin
      WriteLn('Reference: ' + referenceId);
    end;
  if (email.HasPlainTextBody() = True) then
    begin
      WriteLn(email.GetPlainTextBody());
    end
  else
    begin
      if (email.HasHtmlBody() = True) then
        begin
          reader := THtmlToText.Create;
          WriteLn(reader.ToText(email.GetHtmlBody()));
        end
      else
        begin
          WriteLn('No text or HTML body.');
        end;
    end;


  email.Free;
          reader.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.