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

Append a Part to a Multipart MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.AppendPart method, which appends a copy of another Mime as the last direct child. The only argument is the source Mime. Chilkat copies it, so later changes to the source do not affect the appended part.

Background: This is the general way to build up a multipart entity from parts you construct yourself — a text body, an HTML body, a nested multipart. Because it appends a copy, you can reuse and modify the source object afterward, or append it more than once, without surprises. For attaching a file directly, AppendPartFromFile is more convenient.

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

begin
  success := False;

  //  Demonstrates the Mime.AppendPart method, which appends a copy of another Mime as the last direct
  //  child.  The only argument is the Mime to append.  Chilkat copies it, so later changes to the
  //  source do not affect the appended part.

  mime := TMime.Create;
  success := mime.NewMultipartMixed();
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Build a part and append a copy of it.
  part := TMime.Create;
  success := part.SetBodyFromPlainText('This is an appended part.');
  if (success = False) then
    begin
      WriteLn(part.LastErrorText);
      Exit;
    end;

  success := mime.AppendPart(part);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  WriteLn('Number of parts: ' + mime.NumParts);


  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.