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

Remove a Part from a Multipart MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.RemovePart method, which removes the direct child at a zero-based index. The only argument is the index; valid values are 0 through NumParts - 1. An out-of-range index returns false and changes nothing.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: Removing a part is how you strip an attachment or drop an unwanted alternative when reprocessing a message. Because parts are addressed by position, be mindful that removing one shifts the indexes of those after it — when deleting several, iterate from the last index downward so earlier removals do not renumber the parts you still intend to remove.

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.Mime;

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

procedure RunDemo;
var
  success: Boolean;
  mime: TMime;
  lastIndex: Integer;

begin
  success := False;

  mime := TMime.Create;
  success := mime.LoadMimeFile('qa_data/multipart_message.eml');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Remove the direct child part at a zero-based index.  Valid indexes are 0 through NumParts - 1.
  //  Here the last part is removed.
  lastIndex := mime.NumParts - 1;
  success := mime.RemovePart(lastIndex);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  WriteLn('Parts remaining: ' + mime.NumParts);


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