Sample code for 30+ languages & platforms
Unicode C++

IMAP Download and Verify Signed (S/MIME) Email

See more IMAP Examples

Demonstrates how to download and verify digitally signed S/MIME email.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkImapW.h>
#include <CkMessageSetW.h>
#include <CkEmailW.h>
#include <CkCertW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkImapW imap;

    // Connect to an IMAP server.
    // Use TLS
    imap.put_Ssl(true);
    imap.put_Port(993);
    success = imap.Connect(L"imap.example.com");
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    success = imap.Login(L"myLogin",L"myPassword");
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    // Select an IMAP mailbox
    success = imap.SelectMailbox(L"Inbox");
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    // We can choose to fetch UIDs or sequence numbers.
    bool fetchUids = true;

    // Get the message IDs of all the emails in the mailbox
    CkMessageSetW messageSet;
    success = imap.QueryMbx(L"ALL",fetchUids,messageSet);
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    CkEmailW email;
    CkCertW cert;

    int i = 0;
    while (i < messageSet.get_Count()) {

        const wchar_t *uid = messageSet.GetId(i);
        wprintf(L"uid: %s\n",uid);

        success = imap.FetchEmail(false,uid,true,email);
        if (success == false) {
            wprintf(L"%s\n",imap.lastErrorText());
            return;
        }

        // The security layers of signed and/or encrypted emails
        // are automatically "unwrapped" when loaded into
        // a Chilkat email object.
        // An application only needs to check to see if an email
        // was received signed or encrypted, and then examine
        // the success/failure.  For example:
        if (email.get_ReceivedSigned() == true) {

            wprintf(L"This email was signed.\n");

            // Check to see if the signatures were verified.
            if (email.get_SignaturesValid() == true) {
                wprintf(L"Digital signature(s) verified.\n");
                wprintf(L"Signer: %s\n",email.signedBy());

                // Get the certificate used for signing.
                success = email.LastSignerCert(0,cert);

                if (success == false) {
                    wprintf(L"Failed to get signing certificate object.\n");
                }
                else {
                    wprintf(L"Signing cert: %s\n",cert.subjectCN());
                }

            }
            else {
                wprintf(L"Digital signature verification failed.\n");
            }

        }

        i = i + 1;
    }

    // Disconnect from the IMAP server.
    success = imap.Disconnect();
    }