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

Read an attached email

See more Email Object Examples

Open forward-as-attachment.eml from the forwarding example. Read the cover note’s subject, then open each attached message and inspect its own headers and attachment count. This works even when the outer message has no ordinary files.

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;

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

procedure RunDemo;
var
  success: Boolean;
  wrapper: TEmail;
  nested: TEmail;
  i: Integer;

begin
  success := False;

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

  wrapper := TEmail.Create;
  success := wrapper.LoadEml('forward-as-attachment.eml');
  if (success <> True) then
    begin
      WriteLn(wrapper.LastErrorText);
      Exit;
    end;
  WriteLn('Cover note: ' + wrapper.Subject);
  if (wrapper.NumAttachedMessages = 0) then
    begin
      WriteLn('No attached messages.');
      Exit;
    end;
  nested := TEmail.Create;
  i := 0;
  while (i < wrapper.NumAttachedMessages) do
    begin
      success := wrapper.GetAttachedEmail(i,nested);
      if (success <> True) then
        begin
          WriteLn(wrapper.LastErrorText);
          Exit;
        end;
      WriteLn('Original author: ' + nested.From);
      WriteLn('Original subject: ' + nested.Subject);
      WriteLn('Files on the original: ' + nested.NumAttachments);
      i := i + 1;
    end;



  wrapper.Free;
  nested.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.