React Native Requires Chilkat v11.0.0+
React Native
Using Client Certificate w/ IMAP SSL
Demonstrates how to use a client-side certificate with an IMAP SSL connection. The SetSslClientCert method is called to specify a certificate to be used for the SSL connection.Chilkat React Native Downloads
import { Cert, CertStore, Email, EmailBundle, Imap, JsonObject, MessageSet } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const imap = new Imap();
// To use a secure SSL connection, set SSL and the port:
imap.ssl = true;
// The typical port for IMAP SSL is 993
imap.port = 993;
// Load a certificate from a PFX file and use it.
// Note: Other methods are available to load pre-installed
// certificates from registry-based certificate stores.
// Create an instance of a certificate store object, load a PFX file,
// locate the certificate we need, and use it for signing.
// (a PFX file may contain more than one certificate.)
const certStore = new CertStore();
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
try {
certStore.loadPfxFile('myCertWithPrivateKey.pfx', 'secret');
} catch {
console.log(certStore.lastErrorText);
return;
}
// Find the certificate by the subject common name:
const jsonCN = new JsonObject();
jsonCN.updateString('CN', 'cert common name');
const cert = new Cert();
try {
certStore.findCert(jsonCN, cert);
} catch {
console.log(certStore.lastErrorText);
return;
}
// If a PFX file is known to contain a single certificate,
// you may load it directly into a Chilkat certificate object.
// This snippet of source code shows how:
const cert2 = new Cert();
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
try {
cert2.loadPfxFile('myClientCert.pfx', 'secret');
} catch {
console.log(cert.lastErrorText);
return;
}
// Use the cert:
imap.setSslClientCert(cert);
// Connect to an IMAP server.
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;
}
// Get the message IDs of all the emails in the mailbox
const fetchUids = true;
const messageSet = new MessageSet();
try {
await imap.queryMbxAsync('ALL', fetchUids, messageSet);
} catch {
console.log(imap.lastErrorText);
return;
}
// Fetch the emails into a bundle object:
const bundle = new EmailBundle();
const headersOnly = false;
try {
await imap.fetchMsgSetAsync(headersOnly, messageSet, bundle);
} catch {
console.log(imap.lastErrorText);
return;
}
// Loop over the bundle and display the FROM and SUBJECT of each.
const email = new Email();
let i = 0;
const numEmails = bundle.messageCount;
while (i < numEmails) {
bundle.emailAt(i, email);
console.log(email.from);
console.log(email.subject);
console.log('--');
i++;
}
// Disconnect from the IMAP server.
await imap.disconnectAsync();
}