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

Add an attachment and an inline image

See more Email Object Examples

Add a file attachment from memory and embed an image in the HTML body using its Content-ID. Start with any email that has an HTML body, or use text-and-html.eml from the preceding example. Save the result as with-attachments.eml.

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('text-and-html.eml');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final data = CkBinData();
  try {
    data.appendString('Time,Activity\r\n09:00,Welcome\r\n10:00,Field session\r\n', 'utf-8');
  } on ChilkatException {
    print('Failed: data.AppendString.');
    return;
  }

  try {
    email.addAttachmentBd('agenda.csv', data, 'text/csv');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // AddRelatedBd returns the identifier that must appear after cid: in the HTML.
  final image = CkBinData();
  try {
    image.appendEncoded('iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==', 'base64');
  } on ChilkatException {
    print('Failed: image.AppendEncoded.');
    return;
  }

  final String cid;
  try {
    cid = email.addRelatedBd('confirmed.png', image);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final html = CkStringBuilder();
  html.append('<p><img alt=\'Confirmed\' src="cid:');
  html.append(cid);
  html.append('"></p>');
  html.append(email.getHtmlBody());
  try {
    email.addHtmlAlternativeBody(html.getAsString());
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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

  print('Saved with-attachments.eml.');
}