Sample code for 30+ languages & platforms
React Native

Find Certificate for Email Encryption

Demonstrates finding the recipient's certificate in the Windows certificate store and using it to send encrypted email.

Chilkat React Native Downloads

React Native
import { Cert, Email, MailMan } 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.

  const mailman = new MailMan();

  // Set the SMTP server.
  mailman.smtpHost = 'smtp.example.com';

  // Create a new email object
  const email = new Email();

  email.subject = 'This email is encrypted';
  email.body = 'This is a digitally encrypted mail';
  email.from = 'Joe <joe@example.com>';
  // Emails are encrypted using the recipient's certificate.
  const recipientEmailAddr = 'jane@example2.com';
  email.addTo('Jane', recipientEmailAddr);

  // Indicate that the email is to be sent encrypted.
  email.sendEncrypted = true;

  // This example demonstrates finding the email encryption certificate
  // on a Windows system where the certificate is stored in the Windows 
  // certificate store.

  const cert = new Cert();
  // The recipient's certificate is used to encrypt.
  // (Because the recipient is the only one in possession of the private key to decrypt.)
  try {
    cert.loadByEmailAddress(recipientEmailAddr);
  } catch {
    console.log(cert.lastErrorText);
    return;
  }

  // Specify the certificate to be used for encryption.
  email.setEncryptCert(cert);

  try {
    await mailman.sendEmailAsync(email);
    console.log('Encrypted Mail Sent!');
  } catch {
    console.log(mailman.lastErrorText);
  }
}