React Native
React Native
Send Encrypted Email to Multiple Recipients
Demonstrates how to create and send an S/MIME encrypted email to multiple recipients. The digital certificate of each recipient is required. The encrypting/sending process uses each recipient's digital certificate (which internally contains the public key). Each recipient decrypts the received email using his/her private key.Chilkat React Native Downloads
import { Cert, Email, MailMan } from '@chilkat/react-native'
async function chilkatExample() {
let success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The mailman object is used for sending and receiving email.
const mailman = new MailMan();
// Set the SMTP server.
mailman.smtpHost = 'smtp.mymailserver.com';
// Load each recipient's certificate into a Chilkat certificate object.
// This example loads the certificates from files. However, the Chilkat
// certificate object provides other means for loading certificates,
// such as from in-memory PEM strings, or in-memory binary DER encoded form, etc.
const cert1 = new Cert();
success = true;
try {
cert1.loadFromFile('recipient1.cer');
} catch {
success = false;
}
if (!success) {
console.log(cert1.lastErrorText);
return;
}
const cert2 = new Cert();
success = true;
try {
cert2.loadFromFile('recipient2.cer');
} catch {
success = false;
}
if (!success) {
console.log(cert2.lastErrorText);
return;
}
const cert3 = new Cert();
success = true;
try {
cert3.loadFromFile('recipient3.cer');
} catch {
success = false;
}
if (!success) {
console.log(cert3.lastErrorText);
return;
}
// Create a new email object
const email = new Email();
email.subject = 'This email is encrypted and sent to 3 recipients';
email.body = 'This is an S/MIME encrypted mail sent to 3 recipients';
email.from = 'Chilkat Support <support@chilkatsoft.com>';
// Make each of the certificates available for encrypting the email
// by calling AddEncryptCert for each.
success = true;
try {
email.addEncryptCert(cert1);
} catch {
success = false;
}
if (success) {
success = true;
try {
email.addEncryptCert(cert2);
} catch {
success = false;
}
}
if (success) {
success = true;
try {
email.addEncryptCert(cert3);
} catch {
success = false;
}
}
if (!success) {
console.log(email.lastErrorText);
return;
}
// Add 3 recipients to the email (2 TO addresses, and 1 CC address)
email.addTo('Recipient 1', 'admin@cknotes.com');
email.addTo('Recipient 2', 'somebody001122@yahoo.com');
success = true;
try {
email.addCC('Recipient 3', 'somebody123xyz@gmail.com');
} catch {
success = false;
}
// Indicate that the email is to be sent encrypted.
email.sendEncrypted = true;
// Send the encrypted email...
success = true;
try {
await mailman.sendEmailAsync(email);
} catch {
success = false;
}
if (!success) {
console.log(mailman.lastErrorText);
} else {
console.log('Encrypted Email Sent!');
}
}