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

Make a Copy of an Email

See more Email Object Examples

Demonstrates the Chilkat Email.MakeCopy method, which copies the entire state of this email into another Email object — message content, recipients, headers, body representations, attachments, and related items. This example copies an email and reads a couple of fields from the copy.

Background: A deep copy gives you an independent duplicate: changes to one object do not affect the other. This is handy for template-and-tweak workflows — build a base message once, copy it per recipient, then vary only the recipient or a few fields — without risk of one send's edits bleeding into the next.

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;
  copy: TEmail;

begin
  success := False;

  //  Demonstrates the MakeCopy method, which copies the entire state of this email into
  //  another Email object -- message content, recipients, headers, bodies, attachments, and
  //  related items.

  email := TEmail.Create;
  email.Subject := 'Original';
  email.From := 'alice@example.com';
  email.AddTo('Bob','bob@example.com');
  email.Body := 'Original body.';

  //  Copy the entire email into a new Email object.
  copy := TEmail.Create;

  success := email.MakeCopy(copy);
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  WriteLn('Copy subject: ' + copy.Subject);
  WriteLn('Copy NumTo: ' + copy.NumTo);


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