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

Preserve the original email and save a modified copy

See more Email Object Examples

Use with-attachments.eml as a stand-in for bytes from an IMAP fetch or a message queue. Preserve those bytes first. Parse them into an Email object, add a local processing header, and serialize the result into a separate buffer. The archived input remains suitable for later byte-level inspection.

Chilkat Dart Downloads

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

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

  final mimeBytes = CkBinData();
  try {
    mimeBytes.loadFile('with-attachments.eml');
  } on ChilkatException {
    print('Failed: mimeBytes.LoadFile.');
    return;
  }

  try {
    mimeBytes.writeFile('received-original.eml');
  } on ChilkatException {
    print('Failed: mimeBytes.WriteFile.');
    return;
  }

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

  email.addHeaderField('X-Workflow-State', 'reviewed');
  // GetMimeBd appends to its destination. Use a fresh buffer for this message.
  final rendered = CkBinData();
  try {
    email.getMimeBd(rendered);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    rendered.writeFile('reviewed.eml');
  } on ChilkatException {
    print('Failed: rendered.WriteFile.');
    return;
  }

  print('Archived received-original.eml and saved reviewed.eml.');
}