React Native Requires Chilkat v11.0.0+
React Native
Sorting Email
Demonstrates how to sort an email bundle.Chilkat React Native Downloads
import { Email, EmailBundle, Imap, MessageSet } from '@chilkat/react-native'
async function chilkatExample() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const imap = new Imap();
// Connect to the IMAP server.
// Use TLS
imap.ssl = true;
imap.port = 993;
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;
}
const fetchUids = true;
const messageSet = new MessageSet();
try {
await imap.queryMbxAsync('ALL', fetchUids, messageSet);
} catch {
console.log(imap.lastErrorText);
return;
}
// Fetch the email headers into a bundle object:
const bundle = new EmailBundle();
const headersOnly = true;
try {
await imap.fetchMsgSetAsync(headersOnly, messageSet, bundle);
} catch {
console.log(imap.lastErrorText);
return;
}
// Sort the email bundle by date, recipient, sender, or subject:
const ascending = true;
bundle.sortByDate(ascending);
// To sort by recipient, sender, or subject, call
// SortBySender, SortByRecipient, or SortBySubject.
// Display the Subject and From of each email.
const email = new Email();
let i = 0;
while (i < bundle.messageCount) {
bundle.emailAt(i, email);
console.log(email.getHeaderField('Date'));
console.log(email.subject);
console.log(email.from);
console.log('--');
i++;
}
// Disconnect from the IMAP server.
await imap.disconnectAsync();
}