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

Load an Email from a .eml File

See more Email Object Examples

Demonstrates the Chilkat Email.LoadEml method, which loads a complete email from an .eml file. An EML file contains RFC822/MIME message text. On success, the loaded message replaces the entire current email — recipients, headers, bodies, related items, and attachments. This example loads an EML and reads its subject and from.

Background: .eml is the standard on-disk format for a single email — it is simply the raw MIME saved to a file, which most mail clients can export and open. LoadEml parses that MIME into a fully populated Email object, giving you programmatic access to every part. It is the natural counterpart to SaveEml.

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;

begin
  success := False;

  //  Demonstrates the LoadEml method, which loads a complete email from an .eml file (an EML
  //  file contains RFC822/MIME message text).  On success, the loaded message replaces the
  //  entire current email.

  email := TEmail.Create;

  success := email.LoadEml('qa_data/eml/message.eml');
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  WriteLn('Subject: ' + email.Subject);
  WriteLn('From: ' + email.From);

  //  Note: The path "qa_data/..." is a relative local filesystem path,
  //  relative to the current working directory of the running application.


  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.