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

Remove a Single Attached Message from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveAttachedMessage method, which removes the Nth message/rfc822 sub-part (an attached email) from the message. Indexing begins at 0. This example attaches an email, removes it, and prints the attached-message count before and after.

Background: Attached messages (forwarded emails carried as message/rfc822 parts) are counted and indexed separately from ordinary file attachments and related items. RemoveAttachedMessage targets just one nested email by index — useful, for example, when trimming a bundled digest or stripping a forwarded original before re-sending.

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

begin
  success := False;

  //  Demonstrates the RemoveAttachedMessage method, which removes the Nth message/rfc822
  //  sub-part (attached email) of the email.  The index is zero-based.

  //  Build an inner email and attach it to an outer email.
  innerEmail := TEmail.Create;
  innerEmail.Subject := 'Embedded message';
  innerEmail.From := 'alice@example.com';

  email := TEmail.Create;
  email.Subject := 'Has an attached message';
  success := email.AttachEmail(innerEmail);
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

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

  //  Remove the first attached message (index 0).
  email.RemoveAttachedMessage(0);

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


  innerEmail.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.