Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oMail
LOCAL oEmail
LOCAL oMessage
LOCAL oPrivateKey
LOCAL oSigner
LOCAL cRecipients

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

oMail := CreateObject("Chilkat.MailMan")
oMail:SmtpHost := "smtp.example.com"
oMail:SmtpPort := 465
oMail:SmtpSsl := 1
oMail:RequireSslCertVerify := 1
oMail:SmtpUsername := "SMTP_USERNAME"
oMail:SmtpPassword := "SMTP_PASSWORD"
oEmail := CreateObject("Chilkat.Email")
oEmail:From := "Workshop team <updates@example.com>"
oEmail:Subject := "Workshop registration"
oEmail:Body := "Registration opens on Monday."
nSuccess := oEmail:AddTo("Newsletter editor", "editor@example.net")
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oMail:destroy()
    oEmail:destroy()
    RETURN
ENDIF

//  Render after all headers, recipients, and content are final.
oMessage := CreateObject("Chilkat.BinData")
nSuccess := oMail:RenderToMimeBd(oEmail, oMessage)
IF (nSuccess != 1)
    ? oMail:LastErrorText
    oMail:destroy()
    oEmail:destroy()
    oMessage:destroy()
    RETURN
ENDIF

//  Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
oPrivateKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oPrivateKey:LoadAnyFormatFile("dkim-private.pem", "")
IF (nSuccess != 1)
    ? oPrivateKey:LastErrorText
    oMail:destroy()
    oEmail:destroy()
    oMessage:destroy()
    oPrivateKey:destroy()
    RETURN
ENDIF

oSigner := CreateObject("Chilkat.Dkim")
oSigner:DkimDomain := "example.com"
oSigner:DkimSelector := "mail2026"
oSigner:DkimAlg := "rsa-sha256"
nSuccess := oSigner:SetDkimPrivateKey(oPrivateKey)
IF (nSuccess != 1)
    ? oSigner:LastErrorText
    oMail:destroy()
    oEmail:destroy()
    oMessage:destroy()
    oPrivateKey:destroy()
    oSigner:destroy()
    RETURN
ENDIF

nSuccess := oSigner:DkimSign(oMessage)
IF (nSuccess != 1)
    ? oSigner:LastErrorText
    oMail:destroy()
    oEmail:destroy()
    oMessage:destroy()
    oPrivateKey:destroy()
    oSigner:destroy()
    RETURN
ENDIF

//  archive@example.com is an envelope-only blind copy.
cRecipients := "editor@example.net,archive@example.com"
nSuccess := oMail:SendMimeBd("updates@example.com", cRecipients, oMessage)
IF (nSuccess != 1)
    ? oMail:LastErrorText
    oMail:destroy()
    oEmail:destroy()
    oMessage:destroy()
    oPrivateKey:destroy()
    oSigner:destroy()
    RETURN
ENDIF

? "SMTP relay accepted the signed message."
nSuccess := oMail:CloseSmtpConnection()
IF (nSuccess != 1)
    ? "The SMTP session did not close cleanly."
ENDIF

oMail:destroy()
oEmail:destroy()
oMessage:destroy()
oPrivateKey:destroy()
oSigner:destroy()