Sample code for 30+ languages & platforms
Delphi DLL 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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
email: HCkEmail;
preview: HCkEmail;
html: HCkStringBuilder;

begin
success := False;

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

email := CkEmail_Create();
success := CkEmail_LoadEml(email,'with-attachments.eml');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
preview := CkEmail_Create();
success := CkEmail_MakeCopy(email,preview);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
//  Keep related items: the HTML still refers to them.
CkEmail_DropAttachments(preview);
Memo1.Lines.Add('Files in original: ' + IntToStr(CkEmail_getNumAttachments(email)) + '  Files in preview: ' + IntToStr(CkEmail_getNumAttachments(preview)));
if (CkEmail_HasHtmlBody(preview) <> True) then
  begin
    Memo1.Lines.Add('This message has no HTML preview.');
    Exit;
  end;
html := CkStringBuilder_Create();
success := CkEmail_GetHtmlBodySb(preview,True,html);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(preview));
    Exit;
  end;
success := CkStringBuilder_WriteFile(html,'preview.html','utf-8',False);
if (success <> True) then
  begin
    Memo1.Lines.Add('Failed: html.WriteFile.');
    Exit;
  end;
Memo1.Lines.Add('Saved preview.html.');

CkEmail_Dispose(email);
CkEmail_Dispose(preview);
CkStringBuilder_Dispose(html);