Dart Requires Chilkat v11.0.0+
Dart
IMAP Delete Old Email (before a specified date)
See more IMAP Examples
Demonstrates how to delete email older than a particular date.Chilkat Dart Downloads
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.
final imap = CkImap();
// Connect to an IMAP server.
// Use TLS
imap.ssl = true;
imap.port = 993;
// Use your IMAP server domain. This example
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;
}
// Get message ID's for emails older than 1/1/2025
final fetchUids = true;
final messageSet = CkMessageSet();
try {
imap.queryMbx('SENTBEFORE 01-Jan-2025', fetchUids, messageSet);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// If desired, we can examine the Subject of each email to be deleted..
final email = CkEmail();
var i = 0;
final count = messageSet.count;
final headerOnly = true;
while (i < count) {
try {
imap.fetchEmail(headerOnly, messageSet.getId(i), true, email);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print(email.localDateStr);
print(email.subject);
i++;
}
// 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;
}
// Disconnect from the IMAP server.
imap.disconnect();
}