Sample code for 30+ languages & platforms
C

Use a PEM TLS Client Certificate

See more SMTP Examples

Demonstrates the Chilkat MailMan.SetSslClientCertPem method, which sets the client-side certificate for SSL/TLS connections, loading it from PEM data or from a PEM file. The first argument may contain the PEM text itself or a path to a PEM file; the second is the PEM password. This example loads the client certificate from a PEM file.

Background: PEM is the familiar Base64 text format bracketed by -----BEGIN CERTIFICATE----- lines, and a single PEM can hold both a certificate and its (optionally encrypted) private key. It is the common format in Unix/OpenSSL environments, whereas PFX is more typical on Windows. Because this method accepts either the PEM text or a filename, you can supply credentials straight from a config value or secret store without writing them to disk.

Chilkat C Downloads

C
#include <C_CkMailMan.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkMailMan mailman;

    success = FALSE;

    //  Demonstrates the MailMan.SetSslClientCertPem method, which sets the client-side
    //  certificate for SSL/TLS connections, loading it from PEM data or from a PEM file.  The
    //  1st argument may contain PEM text or a PEM file path; the 2nd is the PEM password.

    mailman = CkMailMan_Create();

    //  Configure the SMTP server connection.
    CkMailMan_putSmtpHost(mailman,"smtp.example.com");
    CkMailMan_putSmtpPort(mailman,465);
    CkMailMan_putSmtpSsl(mailman,TRUE);
    CkMailMan_putSmtpUsername(mailman,"user@example.com");
    CkMailMan_putSmtpPassword(mailman,"myPassword");

    //  Load the client certificate from a PEM file.
    success = CkMailMan_SetSslClientCertPem(mailman,"qa_data/certs/client.pem","pem_password");
    if (success == FALSE) {
        printf("%s\n",CkMailMan_lastErrorText(mailman));
        CkMailMan_Dispose(mailman);
        return;
    }

    success = CkMailMan_VerifySmtpLogin(mailman);
    if (success == FALSE) {
        printf("%s\n",CkMailMan_lastErrorText(mailman));
        CkMailMan_Dispose(mailman);
        return;
    }

    printf("Connected using a PEM TLS client certificate.\n");

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


    CkMailMan_Dispose(mailman);

    }