React Native
React Native
Copy Email from one IMAP Account to Another
See more IMAP Examples
Demonstrates how to copy the email in a mailbox from one account to another.Chilkat React Native Downloads
import { FileAccess, Imap, MessageSet, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
const imapSrc = new Imap();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Connect to our source IMAP server.
imapSrc.ssl = true;
imapSrc.port = 993;
try {
await imapSrc.connectAsync('MY-IMAP-DOMAIN');
} catch {
console.log(imapSrc.lastErrorText);
return;
}
// Login to the source IMAP server
try {
await imapSrc.loginAsync('MY-IMAP-LOGIN', 'MY-IMAP-PASSWORD');
} catch {
console.log(imapSrc.lastErrorText);
return;
}
const imapDest = new Imap();
// Connect to our destination IMAP server.
imapDest.ssl = true;
imapDest.port = 993;
try {
await imapDest.connectAsync('MY-IMAP-DOMAIN2');
} catch {
console.log(imapDest.lastErrorText);
return;
}
// Login to the destination IMAP server
try {
await imapDest.loginAsync('MY-IMAP-LOGIN2', 'MY-IMAP-PASSWORD2');
} catch {
console.log(imapDest.lastErrorText);
return;
}
// Select a source IMAP mailbox on the source IMAP server
try {
await imapSrc.selectMailboxAsync('Inbox');
} catch {
console.log(imapSrc.lastErrorText);
return;
}
const fetchUids = true;
// Get the set of UIDs for all emails on the source server.
let mset: MessageSet;
try {
mset = await imapSrc.searchAsync('ALL', fetchUids);
} catch {
console.log(imapSrc.lastErrorText);
return;
}
// Load the complete set of UIDs that were previously copied.
// We dont' want to copy any of these to the destination.
const fac = new FileAccess();
const msetAlreadyCopied = new MessageSet();
let strMsgSet = fac.readEntireTextFile('qa_cache/saAlreadyLoaded.txt', 'utf-8');
if (fac.lastMethodSuccess) {
msetAlreadyCopied.fromCompactString(strMsgSet);
}
const numUids = mset.count;
const sbFlags = new StringBuilder();
let i = 0;
while (i < numUids) {
// If this UID was not already copied...
const uid = mset.getId(i);
if (!msetAlreadyCopied.containsId(uid)) {
console.log(`copying ${uid}...`);
// Get the flags.
let flags: string;
try {
flags = await imapSrc.fetchFlagsAsync(uid, true);
} catch {
console.log(imapSrc.lastErrorText);
return;
}
sbFlags.setString(flags);
// Get the MIME of this email from the source.
let mimeStr: string;
try {
mimeStr = await imapSrc.fetchSingleAsMimeAsync(uid, true);
} catch {
console.log(imapSrc.lastErrorText);
return;
}
const seen = sbFlags.contains('\\Seen', false);
const flagged = sbFlags.contains('\\Flagged', false);
const answered = sbFlags.contains('\\Answered', false);
const draft = sbFlags.contains('\\Draft', false);
try {
await imapDest.appendMimeWithFlagsAsync('Inbox', mimeStr, seen, flagged, answered, draft);
} catch {
console.log(imapDest.lastErrorText);
return;
}
// Update msetAlreadyCopied with the uid just copied.
msetAlreadyCopied.insertId(uid);
// Save at every iteration just in case there's a failure..
strMsgSet = msetAlreadyCopied.toCompactString();
fac.writeEntireTextFile('qa_cache/saAlreadyLoaded.txt', strMsgSet, 'utf-8', false);
}
i++;
}
// Disconnect from the IMAP servers.
await imapSrc.disconnectAsync();
await imapDest.disconnectAsync();
}