Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
Sign an email and send the signed bytes over SMTP
See more DKIM Examples
Compose the email, render it to MIME once, add its DKIM signature, and submit those same bytes to an SMTP server. Put blind-copy recipients in the SMTP envelope only so their addresses stay out of the visible headers.
Use an authorized SMTP account and your own sender domain. Publish the selector’s matching public key first. If your relay rewrites signed content, have it apply the signature after its final edits. A successful SMTP submission means the relay accepted the message. A recipient can then verify it with the public key in DNS.
Chilkat Unicode C++ Downloads
#include <CkMailManW.h>
#include <CkEmailW.h>
#include <CkBinDataW.h>
#include <CkPrivateKeyW.h>
#include <CkDkimW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkMailManW mail;
mail.put_SmtpHost(L"smtp.example.com");
mail.put_SmtpPort(465);
mail.put_SmtpSsl(true);
mail.put_RequireSslCertVerify(true);
mail.put_SmtpUsername(L"SMTP_USERNAME");
mail.put_SmtpPassword(L"SMTP_PASSWORD");
CkEmailW email;
email.put_From(L"Workshop team <updates@example.com>");
email.put_Subject(L"Workshop registration");
email.put_Body(L"Registration opens on Monday.");
success = email.AddTo(L"Newsletter editor",L"editor@example.net");
if (success != true) {
wprintf(L"%s\n",email.lastErrorText());
return;
}
// Render after all headers, recipients, and content are final.
CkBinDataW message;
success = mail.RenderToMimeBd(email,message);
if (success != true) {
wprintf(L"%s\n",mail.lastErrorText());
return;
}
// Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
CkPrivateKeyW privateKey;
success = privateKey.LoadAnyFormatFile(L"dkim-private.pem",L"");
if (success != true) {
wprintf(L"%s\n",privateKey.lastErrorText());
return;
}
CkDkimW signer;
signer.put_DkimDomain(L"example.com");
signer.put_DkimSelector(L"mail2026");
signer.put_DkimAlg(L"rsa-sha256");
success = signer.SetDkimPrivateKey(privateKey);
if (success != true) {
wprintf(L"%s\n",signer.lastErrorText());
return;
}
success = signer.DkimSign(message);
if (success != true) {
wprintf(L"%s\n",signer.lastErrorText());
return;
}
// archive@example.com is an envelope-only blind copy.
const wchar_t *recipients = L"editor@example.net,archive@example.com";
success = mail.SendMimeBd(L"updates@example.com",recipients,message);
if (success != true) {
wprintf(L"%s\n",mail.lastErrorText());
return;
}
wprintf(L"SMTP relay accepted the signed message.\n");
success = mail.CloseSmtpConnection();
if (success != true) {
wprintf(L"The SMTP session did not close cleanly.\n");
}
}