Sample code for 30+ languages & platforms
Dart

Start IMAP IDLE to Monitor a Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.IdleStart method, which sends the IMAP IDLE command to begin listening for unsolicited mailbox updates. It takes no arguments. A mailbox must first be selected, and the server must advertise the IDLE capability.

Background: IDLE is IMAP's push mechanism: instead of polling for new mail, the client tells the server "notify me when something changes," and the server pushes updates as they happen. This is what enables near-instant new-mail alerts. While IDLE is active, no other IMAP command may be sent on that connection — call IdleDone to leave IDLE before issuing other commands.

Chilkat Dart Downloads

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

void main() {
  // Demonstrates the Imap.IdleStart method, which sends the IMAP IDLE command to begin listening
  // for unsolicited mailbox updates.  It takes no arguments.  A mailbox must be selected and the
  // server must advertise the IDLE capability.

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

  // Enter IDLE mode to monitor the mailbox for changes (new mail, deletions, flag changes).
  try {
    imap.idleStart();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('IDLE mode started.');

  // While IDLE is active, no other IMAP command may be sent.  End IDLE before doing more work.
  try {
    imap.idleDone();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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