React Native Requires Chilkat v11.0.0+
React Native
Read IMAP Email Headers
This example demonstrates how to connect to an IMAP server and download only the email headers for all messages in a mailbox. Downloading headers only is much faster and more efficient than downloading complete emails because the message bodies and attachment contents are not retrieved.
The example shows how to:
- Connect to an IMAP server using SSL/TLS
- Authenticate and select a mailbox
- Query for all messages in the mailbox
- Fetch header-only email information
- Display sender, subject, recipients, and total message size
- Retrieve attachment metadata such as filenames and sizes without downloading the actual attachments
This technique is useful for quickly scanning large mailboxes, building message summaries, or inspecting attachment information while minimizing bandwidth and memory usage.
Chilkat React Native Downloads
import { Email, EmailBundle, Imap, MessageSet } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API has already been unlocked.
// See Global Unlock Sample for example code.
const imap = new Imap();
// Connect to the IMAP server using SSL/TLS on the standard secure IMAP port.
imap.ssl = true;
imap.port = 993;
try {
await imap.connectAsync('imap.example.com');
} catch {
console.log(imap.lastErrorText);
return;
}
// Authenticate with the IMAP server.
try {
await imap.loginAsync('****', '****');
} catch {
console.log(imap.lastErrorText);
return;
}
// Select the mailbox (folder) to access.
try {
await imap.selectMailboxAsync('Inbox');
} catch {
console.log(imap.lastErrorText);
return;
}
// Get the identifiers for all messages in the selected mailbox.
// Setting fetchUids = cktrue causes IMAP UIDs to be returned.
// If ckfalse, IMAP sequence numbers are returned instead.
const fetchUids = true;
const messageSet = new MessageSet();
try {
await imap.queryMbxAsync('ALL', fetchUids, messageSet);
} catch {
console.log(imap.lastErrorText);
return;
}
// Download only the email headers for the messages in the set.
// This is much faster than downloading full emails because the
// message bodies and attachment contents are not retrieved.
//
// Even though the attachments themselves are not downloaded,
// Chilkat can still provide information such as attachment
// filenames, attachment sizes, and the total message size.
const bundle = new EmailBundle();
const headersOnly = true;
try {
await imap.fetchMsgSetAsync(headersOnly, messageSet, bundle);
} catch {
console.log(imap.lastErrorText);
return;
}
// Iterate over each email in the bundle and display summary information.
const email = new Email();
const name = '';
const addr = '';
let i = 0;
let j = 0;
while (i < bundle.messageCount) {
bundle.emailAt(i, email);
// Display the sender and subject line.
console.log(email.from);
console.log(email.subject);
// Display all "To" recipients.
j = 0;
while (j < email.numTo) {
console.log(`TO: ${email.getToName(j)}, ${email.getToAddr(j)}`);
j++;
}
// Display all "CC" recipients.
j = 0;
while (j < email.numCC) {
console.log(`CC: ${email.getCcName(j)}, ${email.getCcAddr(j)}`);
j++;
}
// Display the total size of the email message, including
// the body and all attachments, even though only headers
// were downloaded.
console.log(email.size);
// When full emails are downloaded, attachment information
// is accessed using the email.NumAttachments property and
// the email.GetMailAttach* methods.
//
// However, because this example downloaded headers only,
// attachment metadata must be obtained using the IMAP object.
const numAttach = imap.getMailNumAttach(email);
j = 0;
while (j < numAttach) {
// Display the attachment filename.
console.log(imap.getMailAttachFilename(email, j));
// Display the attachment size in bytes.
const attachSize = imap.getMailAttachSize(email, j);
console.log(` size = ${attachSize} bytes`);
j++;
}
console.log('--');
i++;
}
// Disconnect from the IMAP server.
await imap.disconnectAsync();
}