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

Render an Email to MIME Bytes in a BinData

See more SMTP Examples

Demonstrates the Chilkat MailMan.RenderToMimeBd method, which renders an Email object as MIME bytes and appends the result to a BinData, without sending the email. This example renders a message into a BinData and prints the byte count.

Background: This is the binary version of RenderToMime. A BinData holds raw bytes, which is the right container when you want the rendered MIME as binary — to write it directly to a file or socket, hash it, or hand it to another API expecting a byte buffer — rather than as text.

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.MailMan,
  Chilkat.BinData,
  Chilkat.Email;

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

procedure RunDemo;
var
  success: Boolean;
  mailman: TMailMan;
  email: TEmail;
  bdMime: TBinData;

begin
  success := False;

  //  Demonstrates the MailMan.RenderToMimeBd method, which renders an Email object as MIME
  //  bytes and appends the result to a BinData (without sending the email).

  mailman := TMailMan.Create;

  //  Build the email to render.
  email := TEmail.Create;
  email.Subject := 'Rendered email';
  email.From := 'alice@example.com';
  email.AddTo('Bob','bob@example.com');
  email.Body := 'This message is rendered to MIME bytes in a BinData.';

  //  Render the MIME into a BinData.
  bdMime := TBinData.Create;

  success := mailman.RenderToMimeBd(email,bdMime);
  if (success = False) then
    begin
      WriteLn(mailman.LastErrorText);
      Exit;
    end;

  WriteLn('Rendered MIME size (bytes) = ' + bdMime.NumBytes);


  mailman.Free;
  email.Free;
  bdMime.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.