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

Gmail IMAP: Discover Special Folders

See more GMail SMTP/IMAP/POP Examples

Find Gmail special folders from the attributes returned by LIST, using MbxList and Mailboxes.HasFlag. The result maps roles such as Sent and Trash to the server-provided mailbox names. Chilkat decodes mailbox names for use with its mailbox methods.

Folder names can be localized and visibility can differ by account. The role attributes are stable; do not hard-code [Gmail]/Sent Mail or use the deprecated XLIST command. INBOX is a protocol-defined name. Missing map keys indicate folders not advertised by the server.

Use the mailbox that authorized the full-mail scope https://mail.google.com/. Obtain the protocol OAuth token and refresh it before running this example. The token is loaded from gmail-mail-token.json in a private working directory; a narrow Gmail REST token is insufficient.

Google documentation

Chilkat Dart Downloads

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

void main() {
  // Assumes Chilkat is unlocked. See Global Unlock Sample.

  final token = CkJsonObject();
  try {
    token.loadFile('gmail-mail-token.json');
  } on ChilkatException {
    print('Could not load the protocol token from the private working directory.');
    return;
  }

  if ((token.jsonTypeOf('access_token') != 1) || (token.stringOfEquals('access_token', '', true))) {
    print('Authorize or refresh the mailbox first.');
    return;
  }

  final mailbox = 'user@example.com';
  final imap = CkImap();
  imap.ssl = true;
  imap.port = 993;
  imap.requireSslCertVerify = true;
  imap.authMethod = 'XOAUTH2';
  try {
    imap.connect('imap.gmail.com');
  } on ChilkatException {
    print('Could not establish the Gmail IMAP TLS connection.');
    return;
  }

  try {
    imap.login(mailbox, token.stringOf('access_token'));
  } on ChilkatException {
    imap.disconnect();
    print('IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.');
    return;
  }

  final folders = CkMailboxes();
  try {
    imap.mbxList(false, '', '*', folders);
  } on ChilkatException {
    imap.logout();
    imap.disconnect();
    print('Could not list Gmail folders.');
    return;
  }

  // LIST special-use attributes work with localized folder names. XLIST is obsolete.
  final special = CkJsonObject();
  var i = 0;
  while (i < folders.count) {
    if (folders.isSelectable(i)) {
      print('${folders.getName(i)}  ${folders.getFlags(i)}');
      if (folders.hasFlag(i, '\\All')) {
        special.updateString('allMail', folders.getName(i));
      }

      if (folders.hasFlag(i, '\\Sent')) {
        special.updateString('sent', folders.getName(i));
      }

      if (folders.hasFlag(i, '\\Drafts')) {
        special.updateString('drafts', folders.getName(i));
      }

      if (folders.hasFlag(i, '\\Junk')) {
        special.updateString('spam', folders.getName(i));
      }

      if (folders.hasFlag(i, '\\Trash')) {
        special.updateString('trash', folders.getName(i));
      }

      if (folders.hasFlag(i, '\\Flagged')) {
        special.updateString('starred', folders.getName(i));
      }

      if (folders.hasFlag(i, '\\Important')) {
        special.updateString('important', folders.getName(i));
      }
    }

    i++;
  }

  imap.logout();
  imap.disconnect();
  special.emitCompact = false;
  print(special.emit());
  // Missing keys mean the folder was not advertised. Do not substitute an English name.
}