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

Get an Email's MIME into a StringBuilder

See more Email Object Examples

Demonstrates the Chilkat Email.GetMimeSb method, which returns the email as MIME text (header, body or bodies, related items, and all attachments) by appending it to a StringBuilder object. This example builds a message and appends its MIME to a StringBuilder.

Background: This is a StringBuilder-based alternative to GetMime, which returns a plain string. Writing into a StringBuilder is more efficient when the MIME is large or when you intend to further manipulate it — searching, replacing, or accumulating additional text — without creating many intermediate string copies.

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

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

procedure RunDemo;
var
  success: Boolean;
  email: TEmail;
  sbMime: TStringBuilder;

begin
  success := False;

  //  Demonstrates the GetMimeSb method, which returns the email as MIME text (headers, bodies,
  //  related items, and attachments), appending it to a StringBuilder object.

  email := TEmail.Create;
  email.Subject := 'GetMimeSb example';
  email.From := 'alice@example.com';
  email.AddTo('Bob','bob@example.com');
  email.Body := 'Hello!';

  //  Append the complete MIME to a StringBuilder.
  sbMime := TStringBuilder.Create;
  success := email.GetMimeSb(sbMime);
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  WriteLn(sbMime.GetAsString());


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