Sample code for 30+ languages & platforms
Dart

Embed Image in HTML Email

Demonstrates how to create and send an HTML email with an embedded image.

Chilkat Dart Downloads

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

void main() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  // The mailman object is used for sending and receiving email.
  final mailman = CkMailMan();

  // Set the SMTP server.
  mailman.smtpHost = 'smtp.comcast.net';

  // Create a new email object
  final email = CkEmail();

  // Add an embedded image to the HTML email.
  final fileOnDisk = 'images/dude2.gif';
  final filePathInHtml = 'dudeAbc.gif';

  // Embed the GIF image in the email.
  try {
    email.addRelatedFile2(fileOnDisk, filePathInHtml);
  } on ChilkatException {
    print(mailman.lastErrorText);
    return;
  }

  // The src attribute for the image tag is set to the filePathInHtml:
  final htmlBody = '<html><body>Embedded Image:<br><img src="dudeAbc.gif"></body></html>';

  // Set the basic email stuff: HTML body, subject, "from", "to";
  email.setHtmlBody(htmlBody);
  email.subject = 'Dart HTML email with an embedded image.';
  email.addTo('Admin', 'admin@chilkatsoft.com');
  email.from = 'Chilkat Support <support@chilkatsoft.com>';

  try {
    mailman.sendEmail(email);
    print('Mail Sent!');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
  }
}