React Native Requires Chilkat v11.0.0+
React Native
Fetch Oldest/Newest IMAP Email
Emails may be downloaded by sequence number. Assuming the selected mailbox is not empty, the oldest email is at sequence number 1, and the newest email is at sequence number N. The FetchSingle method may be used to download by sequence number.Chilkat React Native Downloads
import { Email, Imap } 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;
try {
await imap.connectAsync('mail.testemail.net');
} catch {
console.log(imap.lastErrorText);
return;
}
// Login
try {
await imap.loginAsync('***', '***');
} catch {
console.log(imap.lastErrorText);
return;
}
// Select an IMAP mailbox
try {
await imap.selectMailboxAsync('Inbox');
} catch {
console.log(imap.lastErrorText);
return;
}
// After selecting a mailbox, the NumMessages property
// contains the number of emails in the selected mailbox.
const n = imap.numMessages;
if (n > 0) {
// The oldest email is always at sequence number 1.
const isUid = false;
const oldestEmail = new Email();
try {
await imap.fetchEmailAsync(false, 1, isUid, oldestEmail);
} catch {
console.log(imap.lastErrorText);
return;
}
// Display the From and Subject
console.log(oldestEmail.fromAddress);
console.log(oldestEmail.subject);
console.log('--');
// The newest email is at sequence number N:
const newestEmail = new Email();
try {
await imap.fetchEmailAsync(false, n, isUid, newestEmail);
} catch {
console.log(imap.lastErrorText);
return;
}
// Display the From and Subject
console.log(newestEmail.fromAddress);
console.log(newestEmail.subject);
}
// Disconnect from the IMAP server.
await imap.disconnectAsync();
}