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

Preserve the original email and save a modified copy

See more Email Object Examples

Use with-attachments.eml as a stand-in for bytes from an IMAP fetch or a message queue. Preserve those bytes first. Parse them into an Email object, add a local processing header, and serialize the result into a separate buffer. The archived input remains suitable for later byte-level inspection.

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.BinData,
  Chilkat.Email;

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

procedure RunDemo;
var
  success: Boolean;
  mimeBytes: TBinData;
  email: TEmail;
  rendered: TBinData;

begin
  success := False;

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

  mimeBytes := TBinData.Create;
  success := mimeBytes.LoadFile('with-attachments.eml');
  if (success <> True) then
    begin
      WriteLn('Failed: mimeBytes.LoadFile.');
      Exit;
    end;
  success := mimeBytes.WriteFile('received-original.eml');
  if (success <> True) then
    begin
      WriteLn('Failed: mimeBytes.WriteFile.');
      Exit;
    end;
  email := TEmail.Create;
  success := email.SetFromMimeBd(mimeBytes);
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  email.AddHeaderField('X-Workflow-State','reviewed');
  //  GetMimeBd appends to its destination. Use a fresh buffer for this message.
  rendered := TBinData.Create;
  success := email.GetMimeBd(rendered);
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  success := rendered.WriteFile('reviewed.eml');
  if (success <> True) then
    begin
      WriteLn('Failed: rendered.WriteFile.');
      Exit;
    end;
  WriteLn('Archived received-original.eml and saved reviewed.eml.');


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