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

Get the HTML Body into a StringBuilder

See more Email Object Examples

Demonstrates the Chilkat Email.GetHtmlBodySb method, which returns the text/html body into a StringBuilder. If the first argument is true, the related images found in the MIME are inlined as base64 data URLs directly within the returned HTML. This example retrieves the HTML body without inlining images.

Background: The inline-images option is useful for producing self-contained HTML that displays correctly in a browser or web view without needing the separate related parts — each cid: reference is replaced by a data: URL holding the image bytes. Be aware this can substantially enlarge the HTML, since base64 embedding adds roughly a third to each image's size. Writing into a StringBuilder is efficient for that potentially large output.

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;
  sbHtml: TStringBuilder;

begin
  success := False;

  //  Demonstrates the GetHtmlBodySb method, which returns the text/html body into a
  //  StringBuilder.  If the first argument is true, related images found in the MIME are
  //  inlined as base64 data URLs directly within the returned HTML.

  email := TEmail.Create;
  email.Subject := 'GetHtmlBodySb example';

  email.SetHtmlBody('<html><body><b>Hello in HTML.</b></body></html>');

  //  Get the HTML body into a StringBuilder (false = do not inline related images).
  sbHtml := TStringBuilder.Create;

  success := email.GetHtmlBodySb(False,sbHtml);
  if (success = False) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;

  WriteLn(sbHtml.GetAsString());


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