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

Delete Individual Emails from POP3 Mailbox

Demonstrates how to delete individual emails from a POP3 mailbox.

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.

  // The mailman object is used for receiving (POP3) 
  // and sending (SMTP) email.
  final mailman = CkMailMan();

  // Set the POP3 server's hostname
  mailman.mailHost = 'pop.example.com';

  // Set the POP3 login/password.
  mailman.popUsername = 'myLogin';
  mailman.popPassword = 'myPassword';

  // Set the ImmediateDelete property = false
  // When the DeleteEmail method is called, the POP3 session
  // will remain open and the emails will be deleted all at once
  // when the session closes.
  mailman.immediateDelete = false;

  // Download email headers.  
  final bundle = CkEmailBundle();
  final keepOnServer = true;
  final headersOnly = true;
  final numBodyLines = 1;
  try {
    mailman.fetchAll(keepOnServer, headersOnly, numBodyLines, bundle);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final email = CkEmail();
  var i = 0;
  while (i < bundle.messageCount) {
    bundle.emailAt(i, email);

    print('From: ${email.from}');
    print('Subject: ${email.subject}');

    // If you decide the email is to be deleted, call DeleteEmail.
    // This marks the email for deletion on the POP3 server.
    try {
      mailman.deleteEmail(email);
    } on ChilkatException catch (e) {
      print(e.lastErrorText);
      return;
    }

    i++;
  }

  // Make sure the POP3 session is ended to finalize the deletes.
  try {
    mailman.pop3EndSession();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Success!');
}