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

Download MIME Source of Emails in IMAP Mailbox

Demonstrates how to download the MIME source for emails on an IMAP server.

Chilkat Dart Downloads

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

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

  final imap = CkImap();

  // Connect to an IMAP server.
  // Use TLS
  imap.ssl = true;
  imap.port = 993;
  try {
    imap.connect('imap.example.com');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Login
  try {
    imap.login('myLogin', 'myPassword');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Select an IMAP mailbox
  try {
    imap.selectMailbox('Inbox');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // The NumMessages property contains the number of messages in the selected mailbox.
  final numMessages = imap.numMessages;
  if (numMessages == 0) {
    print('No messages exist in the Inbox.');
    return;
  }

  final sbMime = CkStringBuilder();
  for (var seqNum = 1; seqNum <= numMessages; seqNum++) {
    sbMime.clear();
    try {
      imap.fetchSingleAsMimeSb(seqNum, false, sbMime);
    } on ChilkatException catch (e) {
      print(e.lastErrorText);
      return;
    }

    // The MIME source of the downloaded email is contained in sbMime.
  }

  // Disconnect from the IMAP server.
  imap.disconnect();
}