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

Load an Email from MIME in a StringBuilder

See more Email Object Examples

Demonstrates the Chilkat Email.SetFromMimeSb method, which loads an email from the MIME text stored in a StringBuilder object. On success, it replaces the entire current email. This example serializes one email into a StringBuilder with GetMimeSb, then reconstructs it in a second object.

Background: This is the StringBuilder counterpart to SetFromMimeText. When the MIME already lives in a StringBuilder — perhaps assembled or edited there — passing it directly avoids converting to an intermediate string, which is more efficient for large messages.

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;
  source: TEmail;
  sbMime: TStringBuilder;
  email: TEmail;

begin
  success := False;

  //  Demonstrates the SetFromMimeSb method, which loads an email from MIME text stored in a
  //  StringBuilder object.  On success, it replaces the entire current email.

  //  Build a source email and get its MIME into a StringBuilder.
  source := TEmail.Create;
  source.Subject := 'Source message';
  source.From := 'alice@example.com';
  source.Body := 'Hello from a StringBuilder.';
  sbMime := TStringBuilder.Create;
  source.GetMimeSb(sbMime);

  //  Load a new email object from the StringBuilder MIME.
  email := TEmail.Create;

  success := email.SetFromMimeSb(sbMime);
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  WriteLn('Loaded subject: ' + email.Subject);


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