Sample code for 30+ languages & platforms
C

Send Encrypted Email using Certificate in Apple Keychain

See more Apple Keychain Examples

Send encrypted (S/MIME) email using a certificate found in the Apple Keychain.

Note: This example requires Chilkat v10.1.2 or later.

Chilkat C Downloads

C
#include <C_CkEmail.h>
#include <C_CkCert.h>
#include <C_CkMailMan.h>
#include <C_CkSecrets.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmail email;
    const char *recipientEmailAddr;
    HCkCert cert;
    HCkMailMan mailman;
    HCkSecrets secrets;
    HCkJsonObject json;

    success = FALSE;

    email = CkEmail_Create();
    CkEmail_putSubject(email,"This email is encrypted");
    CkEmail_putBody(email,"This is a S/MIME encrypted mail");
    CkEmail_putFrom(email,"Joe <joe@example.com>");

    // Emails are encrypted using the recipient's certificate.
    recipientEmailAddr = "jane@example2.com";
    CkEmail_AddTo(email,"",recipientEmailAddr);

    // Indicate that the email is to be sent encrypted.
    CkEmail_putSendEncrypted(email,TRUE);

    cert = CkCert_Create();

    // -----------------------------------------------
    // This example requires Chilkat v10.1.2 or later.
    // -----------------------------------------------

    // The recipient's certificate is used to encrypt.
    // This will load the certificate from the Apple Keychain.
    success = CkCert_LoadByEmailAddress(cert,recipientEmailAddr);
    if (success != TRUE) {
        printf("%s\n",CkCert_lastErrorText(cert));
        CkEmail_Dispose(email);
        CkCert_Dispose(cert);
        return;
    }

    // Specify the certificate to be used for encryption.
    success = CkEmail_SetEncryptCert(email,cert);
    CkEmail_putSendEncrypted(email,TRUE);

    mailman = CkMailMan_Create();
    CkMailMan_putSmtpHost(mailman,"smtp.example.com");
    CkMailMan_putSmtpUsername(mailman,"joe@example.com");
    CkMailMan_putSmtpPort(mailman,587);
    CkMailMan_putStartTLS(mailman,TRUE);

    // We'll get the SMTP password from the Apple Keychain.
    // See how we originally saved the email credentials to the Keychain here:
    // Save Email Credentials in Apple Keychain or Windows Credentials Manager
    secrets = CkSecrets_Create();

    // On Windows, this is the Windows Credentials Manager
    // On MacOS/iOS, it is the Apple Keychain
    CkSecrets_putLocation(secrets,"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.
    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"appName","MyEmailApp");
    CkJsonObject_UpdateString(json,"service","SMTP");
    CkJsonObject_UpdateString(json,"domain","example.com");
    CkJsonObject_UpdateString(json,"username","joe@example.com");

    CkMailMan_putSmtpPassword(mailman,CkSecrets_getSecretStr(secrets,json));

    // Send the encrypted email.
    // The email is encrypted and sent within the SendEmail function.
    success = CkMailMan_SendEmail(mailman,email);
    if (success != TRUE) {
        printf("%s\n",CkMailMan_lastErrorText(mailman));
        CkEmail_Dispose(email);
        CkCert_Dispose(cert);
        CkMailMan_Dispose(mailman);
        CkSecrets_Dispose(secrets);
        CkJsonObject_Dispose(json);
        return;
    }

    printf("Encrypted email sent!\n");


    CkEmail_Dispose(email);
    CkCert_Dispose(cert);
    CkMailMan_Dispose(mailman);
    CkSecrets_Dispose(secrets);
    CkJsonObject_Dispose(json);

    }