Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Create an HTML preview with embedded images

See more Email Object Examples

Make an independent copy of with-attachments.eml, remove ordinary file attachments from the copy, and export its HTML with related images as data URLs. The original retains its attachments. Use a sanitized viewer when showing untrusted HTML.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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;
  preview: TEmail;
  html: TStringBuilder;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.

  email := TEmail.Create;
  success := email.LoadEml('with-attachments.eml');
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  preview := TEmail.Create;
  success := email.MakeCopy(preview);
  if (success <> True) then
    begin
      WriteLn(email.LastErrorText);
      Exit;
    end;
  //  Keep related items: the HTML still refers to them.
  preview.DropAttachments();
  WriteLn('Files in original: ' + email.NumAttachments + '  Files in preview: ' + preview.NumAttachments);
  if (preview.HasHtmlBody() <> True) then
    begin
      WriteLn('This message has no HTML preview.');
      Exit;
    end;
  html := TStringBuilder.Create;
  success := preview.GetHtmlBodySb(True,html);
  if (success <> True) then
    begin
      WriteLn(preview.LastErrorText);
      Exit;
    end;
  success := html.WriteFile('preview.html','utf-8',False);
  if (success <> True) then
    begin
      WriteLn('Failed: html.WriteFile.');
      Exit;
    end;
  WriteLn('Saved preview.html.');


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