React Native Requires Chilkat v11.0.0+
React Native
IMAP Delete Old Email (before a specified date)
See more IMAP Examples
Demonstrates how to delete email older than a particular date.Chilkat React Native Downloads
import { Email, Imap, MessageSet } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const imap = new Imap();
// Connect to an IMAP server.
// Use TLS
imap.ssl = true;
imap.port = 993;
// Use your IMAP server domain. This example
try {
await imap.connectAsync('imap.example.com');
} catch {
console.log(imap.lastErrorText);
return;
}
// Login
try {
await imap.loginAsync('myLogin', 'myPassword');
} catch {
console.log(imap.lastErrorText);
return;
}
// Select an IMAP mailbox
try {
await imap.selectMailboxAsync('Inbox');
} catch {
console.log(imap.lastErrorText);
return;
}
// Get message ID's for emails older than 1/1/2025
const fetchUids = true;
const messageSet = new MessageSet();
try {
await imap.queryMbxAsync('SENTBEFORE 01-Jan-2025', fetchUids, messageSet);
} catch {
console.log(imap.lastErrorText);
return;
}
// If desired, we can examine the Subject of each email to be deleted..
const email = new Email();
let i = 0;
const count = messageSet.count;
const headerOnly = true;
while (i < count) {
try {
await imap.fetchEmailAsync(headerOnly, messageSet.getId(i), true, email);
} catch {
console.log(imap.lastErrorText);
return;
}
console.log(email.localDateStr);
console.log(email.subject);
i++;
}
// Set the Deleted flag for each message:
try {
await imap.setFlagsAsync(messageSet, 'Deleted', 1);
} catch {
console.log(imap.lastErrorText);
return;
}
// Expunge and close the mailbox.
try {
await imap.expungeAndCloseAsync();
} catch {
console.log(imap.lastErrorText);
return;
}
// Disconnect from the IMAP server.
await imap.disconnectAsync();
}