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

Read email headers and display the body as text

See more Email Object Examples

Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.

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;
  }

  print('From: ${email.from}');
  print('Subject: ${email.subject}');
  print('Date: ${email.emailDateStr}');
  var i = 0;
  while (i < email.numTo) {
    print('To: ${email.getToName(i)}  ${email.getToAddr(i)}');
    i++;
  }

  try {
    final referenceId = email.getHeaderField('X-Reference-ID');
    print('Reference: $referenceId');
  } on ChilkatException {
    // email.getHeaderField() failed; continue anyway.
  }

  if (email.hasPlainTextBody()) {
    print(email.getPlainTextBody());
  } else {
    if (email.hasHtmlBody()) {
      final reader = CkHtmlToText();
      print(reader.toText(email.getHtmlBody()));
    } else {
      print('No text or HTML body.');
    }
  }
}