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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // Assumes Chilkat is unlocked. See Global Unlock Sample.

  final email = CkEmail();
  try {
    email.loadEml('with-attachments.eml');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final preview = CkEmail();
  try {
    email.makeCopy(preview);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Keep related items: the HTML still refers to them.
  preview.dropAttachments();
  print('Files in original: ${email.numAttachments}  Files in preview: ${preview.numAttachments}');
  if (!preview.hasHtmlBody()) {
    print('This message has no HTML preview.');
    return;
  }

  final html = CkStringBuilder();
  try {
    preview.getHtmlBodySb(true, html);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    html.writeFile('preview.html', 'utf-8', false);
  } on ChilkatException {
    print('Failed: html.WriteFile.');
    return;
  }

  print('Saved preview.html.');
}