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

Remove All Attached Messages from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveAttachedMessages method, which removes all message/rfc822 sub-parts (attached emails) from the object at once. Ordinary file attachments and related HTML resources are left unchanged. This example attaches two emails, removes them all, and prints the count before and after.

Background: This is the "remove them all" companion to RemoveAttachedMessage. Because it touches only the nested-message parts, you can strip forwarded originals or a digest's members while preserving the message body, its inline images, and any regular attachments — a clean way to keep the carrier message while discarding what it enclosed.

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

begin
  //  Demonstrates the RemoveAttachedMessages method, which removes all message/rfc822
  //  sub-parts (attached emails) from the email at once.  Ordinary file attachments and
  //  related items are not affected.

  inner1 := TEmail.Create;
  inner1.Subject := 'Embedded 1';

  inner2 := TEmail.Create;
  inner2.Subject := 'Embedded 2';

  email := TEmail.Create;
  email.Subject := 'Has attached messages';
  email.AttachEmail(inner1);
  email.AttachEmail(inner2);

  WriteLn('NumAttachedMessages before = ' + email.NumAttachedMessages);

  //  Remove all attached messages.
  email.RemoveAttachedMessages();

  WriteLn('NumAttachedMessages after = ' + email.NumAttachedMessages);


  inner1.Free;
  inner2.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.