Sample code for 30+ languages & platforms
Dart

Store One or More Flags on a Single Message

See more IMAP Examples

Demonstrates the Chilkat Imap.StoreFlags method, which sets or clears one or more flags on a single message identified by ID. The first argument is the message ID, the second (bUid) selects UID vs sequence number, the third is a space-separated list of flag names, and the fourth is the value (1 to set, 0 to clear). This example applies the Seen and Flagged flags to one message by UID.

Background: StoreFlags is the single-message counterpart to SetFlags and lets you change several flags at once by listing them space-separated (without leading backslashes). Because the ID can be either a UID or a sequence number, the bUid argument must match how you obtained it — a UID from a UID search, or a sequence number from a sequence-based operation. UIDs are the safer choice since they remain valid as the mailbox changes.

Chilkat Dart Downloads

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

void main() {
  // Demonstrates the Imap.StoreFlags method, which sets or clears one or more flags on a single
  // message identified by ID.  The 1st argument is the message ID, the 2nd (bUid) selects UID
  // vs sequence number, the 3rd is a space-separated list of flag names, and the 4th is the
  // value (1 to set, 0 to clear).

  final imap = CkImap();

  imap.ssl = true;
  imap.port = 993;

  try {
    imap.connect('imap.example.com');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    imap.login('user@example.com', 'myPassword');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    imap.selectMailbox('Inbox');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Get the UIDs of all messages.
  final msgSet = CkMessageSet();
  try {
    imap.queryMbx('ALL', true, msgSet);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  if (msgSet.count > 0) {
    // Apply the Seen and Flagged flags to the first message.  bUid is true because the ID
    // came from a UID search.
    final uid = msgSet.getId(0);
    try {
      imap.storeFlags(uid, true, 'Seen Flagged', 1);
    } on ChilkatException catch (e) {
      print(e.lastErrorText);
      return;
    }

    print('Updated flags on UID $uid');
  }

  try {
    imap.disconnect();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }
}