React Native Requires Chilkat v11.0.0+
React Native
IMAP Read Encrypted Email
See more IMAP Examples
Demonstrates how to read encrypted email from an IMAP mailbox.Reading encrypted email works the same as reading non-encrypted email. If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.
Information about the original encrypted state of the email is available after it has been downloaded and decrypted.
Chilkat React Native Downloads
import { Cert, Email, Imap, JsonObject, MessageSet, Secrets } from '@chilkat/react-native'
async function chilkatExample() {
const imap = new Imap();
imap.ssl = true;
imap.port = 993;
try {
await imap.connectAsync('imap.example2.com');
} catch {
console.log(imap.lastErrorText);
return;
}
// We'll get the IMAP email account's password from the Apple Keychain or Windows Credentials Manager.
// See how we originally saved the email credentials to the Keychain here:
// Save Email Credentials in Apple Keychain or Windows Credentials Manager
const secrets = new Secrets();
// On Windows, this is the Windows Credentials Manager
// On MacOS/iOS, it is the Apple Keychain
secrets.location = 'local_manager';
// Specify the name of the secret.
// service and username are required.
// appName and domain are optional.
// Note: The values are arbitrary and can be anything you want.
const json = new JsonObject();
json.updateString('appName', 'MyEmailApp');
json.updateString('service', 'IMAP');
json.updateString('domain', 'example2.com');
json.updateString('username', 'jane@example2.com');
let password: string;
try {
password = await secrets.getSecretStrAsync(json);
} catch {
console.log(secrets.lastErrorText);
return;
}
try {
await imap.loginAsync('jane@example2.com', password);
} catch {
console.log(imap.lastErrorText);
return;
}
// Select an IMAP mailbox
try {
await imap.selectMailboxAsync('Inbox');
} catch {
console.log(imap.lastErrorText);
return;
}
// This example: Send Encrypted Email using Certificate in Apple Keychain
// sent an email with the subject "This email is encrypted".
// Let's download an email with the word "encrypted" in the subject.
const messageSet = new MessageSet();
try {
await imap.queryMbxAsync('SUBJECT encrypted', true, messageSet);
} catch {
console.log(imap.lastErrorText);
return;
}
if (messageSet.count === 0) {
console.log('No messages found.');
return;
}
// Reading encrypted email works the same as reading non-encrypted email.
// If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store),
// Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.
const uid = messageSet.getId(0);
const email = new Email();
try {
await imap.fetchEmailAsync(false, uid, true, email);
} catch {
console.log(imap.lastErrorText);
return;
}
// Here we can show if the email was received encrypted, if it was successfully decrypted, and
// which certificate was used to decrypt.
console.log(`Email received encrypted: ${email.receivedEncrypted}`);
// Was it successfully decrypted?
console.log(`Successfully decrypted: ${email.decrypted}`);
// What cert was used to decrypt?
console.log(`Encrypted by: ${email.encryptedBy}`);
const cert = new Cert();
try {
email.lastDecryptCert(cert);
console.log(`Certificate DN: ${cert.subjectDN}`);
} catch {
// email.lastDecryptCert() failed; continue anyway.
}
// Show the decrypted email body.
console.log(email.body);
await imap.disconnectAsync();
}