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

Forward an email as an attachment

See more Email Object Examples

Attach the original Email to a new message as a message/rfc822 part. The recipient can open the attached email and read its original headers and body.

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;
  original: TEmail;
  email: TEmail;

begin
  success := False;

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

  original := TEmail.Create;
  success := original.LoadEml('message.eml');
  if (success <> True) then
    begin
      WriteLn(original.LastErrorText);
      Exit;
    end;
  email := TEmail.Create;
  email.From := 'Sam Lee <sam@example.com>';
  success := email.AddTo('Alex Rivera','alex@example.net');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  email.Subject := 'Forwarded message';
  email.Body := 'The original email is attached.';
  success := email.AttachEmail(original);
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  success := email.SaveEml('forward-as-attachment.eml');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  WriteLn('Saved forward-as-attachment.eml.');


  original.Free;
  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.