Sample code for 30+ languages & platforms
Unicode 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 Unicode C Downloads

Unicode C
#include <C_CkEmailW.h>
#include <C_CkCertW.h>
#include <C_CkMailManW.h>
#include <C_CkSecretsW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmailW email;
    const wchar_t *recipientEmailAddr;
    HCkCertW cert;
    HCkMailManW mailman;
    HCkSecretsW secrets;
    HCkJsonObjectW json;

    success = FALSE;

    email = CkEmailW_Create();
    CkEmailW_putSubject(email,L"This email is encrypted");
    CkEmailW_putBody(email,L"This is a S/MIME encrypted mail");
    CkEmailW_putFrom(email,L"Joe <joe@example.com>");

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

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

    cert = CkCertW_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 = CkCertW_LoadByEmailAddress(cert,recipientEmailAddr);
    if (success != TRUE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkEmailW_Dispose(email);
        CkCertW_Dispose(cert);
        return;
    }

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

    mailman = CkMailManW_Create();
    CkMailManW_putSmtpHost(mailman,L"smtp.example.com");
    CkMailManW_putSmtpUsername(mailman,L"joe@example.com");
    CkMailManW_putSmtpPort(mailman,587);
    CkMailManW_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 = CkSecretsW_Create();

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

    CkMailManW_putSmtpPassword(mailman,CkSecretsW_getSecretStr(secrets,json));

    // Send the encrypted email.
    // The email is encrypted and sent within the SendEmail function.
    success = CkMailManW_SendEmail(mailman,email);
    if (success != TRUE) {
        wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
        CkEmailW_Dispose(email);
        CkCertW_Dispose(cert);
        CkMailManW_Dispose(mailman);
        CkSecretsW_Dispose(secrets);
        CkJsonObjectW_Dispose(json);
        return;
    }

    wprintf(L"Encrypted email sent!\n");


    CkEmailW_Dispose(email);
    CkCertW_Dispose(cert);
    CkMailManW_Dispose(mailman);
    CkSecretsW_Dispose(secrets);
    CkJsonObjectW_Dispose(json);

    }