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

Read IMAP Email Headers

This example demonstrates how to connect to an IMAP server and download only the email headers for all messages in a mailbox. Downloading headers only is much faster and more efficient than downloading complete emails because the message bodies and attachment contents are not retrieved.

The example shows how to:

  • Connect to an IMAP server using SSL/TLS
  • Authenticate and select a mailbox
  • Query for all messages in the mailbox
  • Fetch header-only email information
  • Display sender, subject, recipients, and total message size
  • Retrieve attachment metadata such as filenames and sizes without downloading the actual attachments

This technique is useful for quickly scanning large mailboxes, building message summaries, or inspecting attachment information while minimizing bandwidth and memory usage.

Chilkat Dart Downloads

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

void main() {
  // This example assumes the Chilkat API has already been unlocked.
  // See Global Unlock Sample for example code.

  final imap = CkImap();

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

  // Authenticate with the IMAP server.
  try {
    imap.login('****', '****');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Select the mailbox (folder) to access.
  try {
    imap.selectMailbox('Inbox');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Get the identifiers for all messages in the selected mailbox.
  // Setting fetchUids = cktrue causes IMAP UIDs to be returned.
  // If ckfalse, IMAP sequence numbers are returned instead.
  final fetchUids = true;
  final messageSet = CkMessageSet();
  try {
    imap.queryMbx('ALL', fetchUids, messageSet);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Download only the email headers for the messages in the set.
  // This is much faster than downloading full emails because the
  // message bodies and attachment contents are not retrieved.
  // 
  // Even though the attachments themselves are not downloaded,
  // Chilkat can still provide information such as attachment
  // filenames, attachment sizes, and the total message size.
  final bundle = CkEmailBundle();
  final headersOnly = true;
  try {
    imap.fetchMsgSet(headersOnly, messageSet, bundle);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Iterate over each email in the bundle and display summary information.
  final email = CkEmail();
  final name = '';
  final addr = '';
  var i = 0;
  var j = 0;
  while (i < bundle.messageCount) {
    bundle.emailAt(i, email);

    // Display the sender and subject line.
    print(email.from);
    print(email.subject);

    // Display all "To" recipients.
    j = 0;
    while (j < email.numTo) {
      print('TO: ${email.getToName(j)}, ${email.getToAddr(j)}');
      j++;
    }

    // Display all "CC" recipients.
    j = 0;
    while (j < email.numCC) {
      print('CC: ${email.getCcName(j)}, ${email.getCcAddr(j)}');
      j++;
    }

    // Display the total size of the email message, including
    // the body and all attachments, even though only headers
    // were downloaded.
    print(email.size);

    // When full emails are downloaded, attachment information
    // is accessed using the email.NumAttachments property and
    // the email.GetMailAttach* methods.
    // 
    // However, because this example downloaded headers only,
    // attachment metadata must be obtained using the IMAP object.
    final numAttach = imap.getMailNumAttach(email);

    j = 0;
    while (j < numAttach) {

      // Display the attachment filename.
      print(imap.getMailAttachFilename(email, j));

      // Display the attachment size in bytes.
      final attachSize = imap.getMailAttachSize(email, j);
      print('    size = $attachSize bytes');

      j++;
    }

    print('--');

    i++;
  }

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