Sample code for 30+ languages & platforms
Unicode C

Find Certificate for Email Encryption

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

Chilkat Unicode C Downloads

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

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

    success = FALSE;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    mailman = CkMailManW_Create();

    //  Set the SMTP server.
    CkMailManW_putSmtpHost(mailman,L"smtp.example.com");

    //  Create a new email object
    email = CkEmailW_Create();

    CkEmailW_putSubject(email,L"This email is encrypted");
    CkEmailW_putBody(email,L"This is a digitally 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"Jane",recipientEmailAddr);

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

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

    cert = CkCertW_Create();
    //  The recipient's certificate is used to encrypt.
    //  (Because the recipient is the only one in possession of the private key to decrypt.)
    success = CkCertW_LoadByEmailAddress(cert,recipientEmailAddr);
    if (success != TRUE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkMailManW_Dispose(mailman);
        CkEmailW_Dispose(email);
        CkCertW_Dispose(cert);
        return;
    }

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

    success = CkMailManW_SendEmail(mailman,email);
    if (success != TRUE) {
        wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
    }
    else {
        wprintf(L"Encrypted Mail Sent!\n");
    }



    CkMailManW_Dispose(mailman);
    CkEmailW_Dispose(email);
    CkCertW_Dispose(cert);

    }