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

Get IMAP UID from Email Header

See more IMAP Examples

Demonstrates how to get the IMAP UID from an email header. After fetching an email or header using IMAP, the UID is contained in the ckx-imap-uid header field.

Chilkat Dart Downloads

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

void main() {
  // This example requires 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('login', 'password');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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

  final fetchUids = true;
  // Get the message UIDs of all the emails in the mailbox

  final messageSet = CkMessageSet();
  try {
    imap.queryMbx('ALL', fetchUids, messageSet);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final bundle = CkEmailBundle();
  final headersOnly = true;
  try {
    imap.fetchMsgSet(headersOnly, messageSet, bundle);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // The UID of each fetched email header is available 
  // in the ckx-imap-uid header field.
  final email = CkEmail();
  final msgSet1 = CkMessageSet();
  var i = 0;
  final szBundle = bundle.messageCount;
  while (i < szBundle) {
    bundle.emailAt(i, email);

    // Build a message set containing one UID
    final uidStr = email.getHeaderField('ckx-imap-uid');
    msgSet1.fromCompactString(uidStr);

    // Move this message to some other folder.
    try {
      imap.moveMessages(msgSet1, 'someOtherFolder');
    } on ChilkatException catch (e) {
      print(e.lastErrorText);
      // Exit the loop...
      i = szBundle;
    }

    i++;
  }

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