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

Embed a Message as message/rfc822

See more MIME Examples

Demonstrates the Chilkat Mime.NewMessageRfc822 method, which clears the object and initializes it as a message/rfc822 entity whose body is a serialized copy of another Mime. The only argument is the message to embed.

Background: message/rfc822 wraps a whole email as the body of another — the mechanism behind forwarding a message "as attachment" or bundling one message inside another. The inner message is copied at the time of the call, so later edits to the source do not change the embedded copy. The result is a self-contained part a mail client will present as a nested message.

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;
  innerMsg: TMime;
  mime: TMime;

begin
  success := False;

  //  Demonstrates the Mime.NewMessageRfc822 method, which clears the current object and initializes
  //  it as a message/rfc822 entity whose body is a serialized copy of another Mime.  The only
  //  argument is the Mime to embed.

  //  Build the inner message to embed.
  innerMsg := TMime.Create;
  success := innerMsg.SetHeaderField('Subject','Forwarded message');
  if (success = False) then
    begin
      WriteLn(innerMsg.LastErrorText);
      Exit;
    end;
  success := innerMsg.SetBodyFromPlainText('This is the original message being forwarded.');
  if (success = False) then
    begin
      WriteLn(innerMsg.LastErrorText);
      Exit;
    end;

  //  Wrap it as message/rfc822 (an embedded/forwarded email).
  mime := TMime.Create;
  success := mime.NewMessageRfc822(innerMsg);
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  WriteLn('Content-Type: ' + mime.ContentType);


  innerMsg.Free;
  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.