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

Access a Part in a Multipart MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.PartAt method, which provides live access to the direct child at a zero-based index. The first argument is the index and the second is a Mime object that will reference the child. Modifying it modifies the child stored in the parent tree.

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: This is how you reach into a multipart message to inspect or edit an individual part — read a part's Content-Type, extract an attachment's body, or change a part in place. The key point is that the returned Mime is a live reference, not a copy, so edits to it flow back into the parent tree. Loop from 0 to NumParts - 1 to visit every direct child.

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;
  n: Integer;
  part: TMime;
  i: 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;

  //  PartAt provides LIVE access to a direct child part.  The 1st argument is the zero-based index
  //  and the 2nd is a Mime object that will reference the child.  Modifying it modifies the child in
  //  the parent tree.
  n := mime.NumParts;
  WriteLn('Number of parts: ' + n);

  part := TMime.Create;

  for i := 0 to n - 1 do
    begin
      success := mime.PartAt(i,part);
      if (success = False) then
        begin
          WriteLn(mime.LastErrorText);
          Exit;
        end;
      WriteLn('Part ' + i + ' Content-Type: ' + part.ContentType);
    end;



  mime.Free;
  part.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.