React Native Requires Chilkat v11.0.0+
React Native
Reading Unread POP3 Email
The POP3 protocol cannot determine which emails are "unread," and pure POP3 servers do not store this information. Servers like Exchange Server, offering both POP3 and IMAP interfaces, do contain read/unread data, but it's only accessible through IMAP. Email clients like Outlook and Thunderbird store read/unread statuses on the client side. The example demonstrates using UIDLs to track and manage "unread" emails.Chilkat React Native Downloads
import { Email, FileAccess, Hashtable, MailMan, StringBuilder, StringTable } 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.
// The mailman object is used for receiving (POP3)
// and sending (SMTP) email.
const mailman = new MailMan();
// Set the POP3 server's hostname
mailman.mailHost = 'pop.example.com';
// Set the POP3 login/password.
mailman.popUsername = '***';
mailman.popPassword = '***';
// Keep a records of already-seen UIDLs in hash table serialized to an XML file.
const seenUidlsPath = 'c:/temp/seenUidls.xml';
const sbXml = new StringBuilder();
const htSeenUidls = new Hashtable();
const fac = new FileAccess();
if (fac.fileExists(seenUidlsPath)) {
try {
sbXml.loadFile(seenUidlsPath, 'utf-8');
} catch {
console.log(sbXml.lastErrorText);
return;
}
htSeenUidls.addFromXmlSb(sbXml);
}
// Get the complete list of UIDLs on the mail server.
const stUidls = new StringTable();
try {
await mailman.fetchUidlsAsync(stUidls);
} catch {
console.log(mailman.lastErrorText);
return;
}
// Build a list of unseen UIDLs
const stUnseenUidls = new StringTable();
let uidl = '';
let i = 0;
let count = stUidls.count;
while (i < count) {
uidl = stUidls.stringAt(i);
if (!htSeenUidls.contains(uidl)) {
stUnseenUidls.append(uidl);
}
i++;
}
if (stUnseenUidls.count === 0) {
console.log('No unseen emails!');
return;
}
// Download the unseen emails, adding each UIDL to the "seen" hash table.
const email = new Email();
count = stUnseenUidls.count;
i = 0;
while (i < count) {
// Download the full email.
uidl = stUnseenUidls.stringAt(i);
try {
await mailman.fetchByUidlAsync(uidl, false, 0, email);
} catch {
console.log(mailman.lastErrorText);
return;
}
console.log(i);
console.log(`From: ${email.from}`);
console.log(`Subject: ${email.subject}`);
// Add this UIDL to the "seen" hash table.
htSeenUidls.addStr(uidl, '');
i++;
}
await mailman.pop3EndSessionAsync();
// Update the "seen" UIDLs file.
sbXml.clear();
htSeenUidls.toXmlSb(sbXml);
try {
sbXml.writeFile(seenUidlsPath, 'utf-8', false);
} catch {
console.log(sbXml.lastErrorText);
return;
}
console.log('Success.');
}