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

Fetch 1st N Headers of Search Results

Calls Search to get a message set, then downloads the 1st N messages in the message set. There are two equivalent ways of doing it: (1) iterate from 0 to N-1 and download each message individually, or (2) create a new message set that contains the 1st N messages and pass it to FetchHeaders. Both are demonstrated here.

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('****', '****');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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

  // Get the message IDs of all the emails in the mailbox
  // We can choose to fetch UIDs or sequence numbers.
  final fetchUids = true;
  final messageSet = CkMessageSet();
  try {
    imap.queryMbx('ALL', fetchUids, messageSet);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final numFound = messageSet.count;
  if (numFound == 0) {
    print('No messages found.');
    return;
  }

  // Get the 1st 10 messages in messageSet.
  var upperBound = 10;
  if (numFound < upperBound) {
    upperBound = numFound;
  }

  var i = 0;
  final bUid = messageSet.hasUids;
  final headerOnly = true;
  final email = CkEmail();

  while (i < upperBound) {

    try {
      imap.fetchEmail(headerOnly, messageSet.getId(i), bUid, email);
    } on ChilkatException catch (e) {
      print(e.lastErrorText);
      return;
    }

    print('$i: ${email.subject}');

    i++;
  }

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