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

How to Download Messages in MessageSet One at a Time

See more IMAP Examples

If a message set contains a huge number of emails, it's NOT a good idea to try to download all at once into an email bundle using a method such as FetchBundle. It's better to iterate over the messages in the set to download one by one.

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.

  final imap = CkImap();

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

  // Authenticate
  try {
    imap.login('email_account_login', 'email_account_password');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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

  // Search for messages and return a set of matching messages.
  // (This example will simply search for ALL messages.)
  final fetchUids = true;

  final messageSet = CkMessageSet();
  try {
    imap.queryMbx('ALL', fetchUids, messageSet);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Number of messages = ${messageSet.count}');

  final email = CkEmail();
  var i = 0;
  while (i < messageSet.count) {
    try {
      imap.fetchEmail(false, messageSet.getId(i), fetchUids, email);
    } on ChilkatException catch (e) {
      print(e.lastErrorText);
      return;
    }

    print('${email.from}; ${email.subject}');

    i++;
  }

  print('OK');
}