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

POP3 Verify Signed (S/MIME) Email

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

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkMailManW.h>
#include <CkStringTableW.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.

    CkMailManW mailman;

    // Set the POP3 server's hostname
    mailman.put_MailHost(L"pop.example.com");

    // Set the POP3 login/password.
    mailman.put_PopUsername(L"myLogin");
    mailman.put_PopPassword(L"myPassword");

    CkStringTableW stUidls;
    success = mailman.FetchUidls(stUidls);
    if (success == false) {
        wprintf(L"%s\n",mailman.lastErrorText());
        return;
    }

    CkEmailW email;
    CkCertW cert;

    int count = stUidls.get_Count();
    int i = 0;
    while (i < count) {
        // Download the full email.
        success = mailman.FetchByUidl(stUidls.stringAt(i),false,0,email);
        if (success == false) {
            wprintf(L"%s\n",mailman.lastErrorText());
            return;
        }

        wprintf(L"%d\n",i);
        wprintf(L"From: %s\n",email.ck_from());
        wprintf(L"Subject: %s\n",email.subject());

        // 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());

                success = email.LastSignerCert(0,cert);
                if (success == false) {
                    wprintf(L"%s\n",email.lastErrorText());
                    return;
                }

                wprintf(L"Signing cert: %s\n",cert.subjectCN());
            }

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

        i = i + 1;
    }

    mailman.Pop3EndSession();
    }