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

Imap.GetMailSize vs Email.Size

Shows how to get the total size of an email, as well as the sizes of the attachments. This can be done when either full-emails or headers-only are downloaded.

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;
  }

  // When downloading headers, each email object contains
  // (obviously) the headers, but the body will be missing.
  // Attachments will not be included.  However, it is
  // possible to get information about the attachments
  // as well as the complete size of the email.
  final bundle = CkEmailBundle();
  final headersOnly = true;
  try {
    imap.fetchMsgSet(headersOnly, messageSet, bundle);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Loop over the email objects and display information about each:
  final email = CkEmail();
  var i = 0;
  var j = 0;
  final numEmails = bundle.messageCount;
  while (i < numEmails) {
    bundle.emailAt(i, email);

    // Display the From and Subject
    print(email.from);
    print(email.subject);

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

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

    // Show the total size of the email, including body and attachments:
    print(email.size);

    // When a full email is downloaded, we would use the
    // email.NumAttachments property in conjunction with the
    // email.GetMailAttach* methods.
    // However, when an email object contains only the header,
    // we need to access the attachment info differently:
    final numAttach = imap.getMailNumAttach(email);
    j = 0;
    while (j < numAttach) {
      print(imap.getMailAttachFilename(email, j));
      final attachSize = imap.getMailAttachSize(email, j);
      print('    size = $attachSize bytes');
      j++;
    }

    print('--');

    i++;
  }

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