Sample code for 30+ languages & platforms
Unicode C

Use a TLS Client Certificate

See more SMTP Examples

Demonstrates the Chilkat MailMan.SetSslClientCert method, which sets the client-side certificate to use for SSL/TLS connections. This is typically not required — most SSL/TLS connections authenticate the server only. This example loads a client certificate from a PFX and uses it when connecting.

Background: In an ordinary TLS handshake only the server presents a certificate, and the client proves who it is later with a password. Mutual TLS adds a second leg: the client also presents a certificate, so it is authenticated cryptographically at the transport layer. Some corporate or high-security mail servers require this. Because the client must prove possession of the key, the certificate needs its private key — hence loading from a PFX.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkMailManW mailman;
    HCkCertW cert;

    success = FALSE;

    //  Demonstrates the MailMan.SetSslClientCert method, which sets the client-side certificate
    //  to use for SSL/TLS connections.  This is typically not required -- most SSL/TLS
    //  connections authenticate the server only.

    mailman = CkMailManW_Create();

    //  Configure the SMTP server connection.
    CkMailManW_putSmtpHost(mailman,L"smtp.example.com");
    CkMailManW_putSmtpPort(mailman,465);
    CkMailManW_putSmtpSsl(mailman,TRUE);
    CkMailManW_putSmtpUsername(mailman,L"user@example.com");
    CkMailManW_putSmtpPassword(mailman,L"myPassword");

    //  Load the client certificate (with its private key) from a PFX file.
    cert = CkCertW_Create();
    success = CkCertW_LoadPfxFile(cert,L"qa_data/certs/client.pfx",L"pfx_password");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkMailManW_Dispose(mailman);
        CkCertW_Dispose(cert);
        return;
    }

    //  Use this certificate to identify the client during the TLS handshake.
    success = CkMailManW_SetSslClientCert(mailman,cert);
    if (success == FALSE) {
        wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
        CkMailManW_Dispose(mailman);
        CkCertW_Dispose(cert);
        return;
    }

    success = CkMailManW_VerifySmtpLogin(mailman);
    if (success == FALSE) {
        wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
        CkMailManW_Dispose(mailman);
        CkCertW_Dispose(cert);
        return;
    }

    wprintf(L"Connected using a TLS client certificate.\n");

    //  Note: The path "qa_data/certs/client.pfx" is a relative local filesystem path,
    //  relative to the current working directory of the running application.


    CkMailManW_Dispose(mailman);
    CkCertW_Dispose(cert);

    }