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

Delete IMAP Email

To delete an email, the "Deleted" flag must be set to 1 for each message to be deleted. You must also call Expunge or ExpungeAndClose to remove the emails marked as deleted.

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.

  // This example deletes email matching a criteria from Inbox.
  final imap = CkImap();

  // Turn on session logging:
  imap.keepSessionLog = true;

  // 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('myLogin', 'myPassword');
  } 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 IDs for all emails having "FTP2" in the subject.
  final messageSet = CkMessageSet();
  try {
    imap.queryMbx('SUBJECT FTP2', fetchUids, messageSet);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Set the Deleted flag for each message:
  try {
    imap.setFlags(messageSet, 'Deleted', 1);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Expunge and close the mailbox.
  try {
    imap.expungeAndClose();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Display the session log.
  print(imap.sessionLog);

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