C++ Requires Chilkat v11.0.0+
C++
Verify each DKIM signature in email downloaded over IMAP
See more DKIM Examples
Download the first ten messages (or all messages in a smaller inbox) as complete MIME, including attachments, while preserving their unread flags. The verifier looks up the selector’s TXT record when it needs a key. Inspect VerifyInfo immediately after each attempt for the signing domain, selector, and any body-hash mismatch.
Verify each signature independently, in top-to-bottom order. A passing signature authenticates the signing domain’s responsibility for the signed content. Identifying the author and checking DMARC alignment with the visible From address are separate steps. A missing DNS key or unavailable resolver can also prevent verification. DKIM specification.
Chilkat C++ Downloads
#include <CkImap.h>
#include <CkMessageSet.h>
#include <CkDkim.h>
#include <CkBinData.h>
#include <CkJsonObject.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkImap imap;
imap.put_Ssl(true);
imap.put_Port(993);
imap.put_RequireSslCertVerify(true);
imap.put_PeekMode(true);
imap.put_AutoDownloadAttachments(true);
success = imap.Connect("imap.example.com");
if (success != true) {
std::cout << imap.lastErrorText() << "\r\n";
return;
}
success = imap.Login("IMAP_USERNAME","IMAP_PASSWORD");
if (success != true) {
std::cout << imap.lastErrorText() << "\r\n";
return;
}
success = imap.SelectMailbox("INBOX");
if (success != true) {
std::cout << imap.lastErrorText() << "\r\n";
return;
}
// Select the first ten sequence positions, then fetch by stable UID.
CkMessageSet messages;
success = imap.QueryMbx("1:10",true,messages);
if (success != true) {
std::cout << imap.lastErrorText() << "\r\n";
return;
}
int i = 0;
CkDkim verifier;
CkBinData message;
while ((i < messages.get_Count())) {
unsigned long uid = messages.GetId(i);
std::cout << "Message UID " << uid << "\r\n";
success = imap.FetchSingleBd(uid,true,message);
if (success != true) {
std::cout << imap.lastErrorText() << "\r\n";
}
else {
int count = verifier.NumDkimSigs(message);
if (count == 0) {
std::cout << "No DKIM signatures." << "\r\n";
}
int sig = 0;
while ((sig < count)) {
success = verifier.DkimVerify(sig,message);
// Read diagnostics before the next verification replaces them.
CkJsonObject info;
bool haveInfo = info.Load(verifier.verifyInfo());
if (success == true) {
std::cout << "Signature " << sig << " verified." << "\r\n";
}
else {
if ((haveInfo == true) && (info.StringOfEquals("bodyHashVerified","no",true) == true)) {
std::cout << "Signature " << sig << ": the body hash differs." << "\r\n";
}
else {
std::cout << "Signature " << sig << " could not be verified." << "\r\n";
}
std::cout << verifier.lastErrorText() << "\r\n";
}
if ((haveInfo == true) && (info.JsonTypeOf("domain") == 1)) {
std::cout << "Signing domain: " << info.stringOf("domain") << " Selector: " << info.stringOf("selector") << "\r\n";
}
sig = sig + 1;
}
}
i = i + 1;
}
success = imap.Disconnect();
}