PureBasic Requires Chilkat v11.0.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkBinData.pb"
IncludeFile "CkDkim.pb"
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkMailMan.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
mail.i = CkMailMan::ckCreate()
If mail.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkMailMan::setCkSmtpHost(mail, "smtp.example.com")
CkMailMan::setCkSmtpPort(mail, 465)
CkMailMan::setCkSmtpSsl(mail, 1)
CkMailMan::setCkRequireSslCertVerify(mail, 1)
CkMailMan::setCkSmtpUsername(mail, "SMTP_USERNAME")
CkMailMan::setCkSmtpPassword(mail, "SMTP_PASSWORD")
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkEmail::setCkFrom(email, "Workshop team <updates@example.com>")
CkEmail::setCkSubject(email, "Workshop registration")
CkEmail::setCkBody(email, "Registration opens on Monday.")
success = CkEmail::ckAddTo(email,"Newsletter editor","editor@example.net")
If success <> 1
Debug CkEmail::ckLastErrorText(email)
CkMailMan::ckDispose(mail)
CkEmail::ckDispose(email)
ProcedureReturn
EndIf
; Render after all headers, recipients, and content are final.
message.i = CkBinData::ckCreate()
If message.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkMailMan::ckRenderToMimeBd(mail,email,message)
If success <> 1
Debug CkMailMan::ckLastErrorText(mail)
CkMailMan::ckDispose(mail)
CkEmail::ckDispose(email)
CkBinData::ckDispose(message)
ProcedureReturn
EndIf
; Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
privateKey.i = CkPrivateKey::ckCreate()
If privateKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPrivateKey::ckLoadAnyFormatFile(privateKey,"dkim-private.pem","")
If success <> 1
Debug CkPrivateKey::ckLastErrorText(privateKey)
CkMailMan::ckDispose(mail)
CkEmail::ckDispose(email)
CkBinData::ckDispose(message)
CkPrivateKey::ckDispose(privateKey)
ProcedureReturn
EndIf
signer.i = CkDkim::ckCreate()
If signer.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkDkim::setCkDkimDomain(signer, "example.com")
CkDkim::setCkDkimSelector(signer, "mail2026")
CkDkim::setCkDkimAlg(signer, "rsa-sha256")
success = CkDkim::ckSetDkimPrivateKey(signer,privateKey)
If success <> 1
Debug CkDkim::ckLastErrorText(signer)
CkMailMan::ckDispose(mail)
CkEmail::ckDispose(email)
CkBinData::ckDispose(message)
CkPrivateKey::ckDispose(privateKey)
CkDkim::ckDispose(signer)
ProcedureReturn
EndIf
success = CkDkim::ckDkimSign(signer,message)
If success <> 1
Debug CkDkim::ckLastErrorText(signer)
CkMailMan::ckDispose(mail)
CkEmail::ckDispose(email)
CkBinData::ckDispose(message)
CkPrivateKey::ckDispose(privateKey)
CkDkim::ckDispose(signer)
ProcedureReturn
EndIf
; archive@example.com is an envelope-only blind copy.
recipients.s = "editor@example.net,archive@example.com"
success = CkMailMan::ckSendMimeBd(mail,"updates@example.com",recipients,message)
If success <> 1
Debug CkMailMan::ckLastErrorText(mail)
CkMailMan::ckDispose(mail)
CkEmail::ckDispose(email)
CkBinData::ckDispose(message)
CkPrivateKey::ckDispose(privateKey)
CkDkim::ckDispose(signer)
ProcedureReturn
EndIf
Debug "SMTP relay accepted the signed message."
success = CkMailMan::ckCloseSmtpConnection(mail)
If success <> 1
Debug "The SMTP session did not close cleanly."
EndIf
CkMailMan::ckDispose(mail)
CkEmail::ckDispose(email)
CkBinData::ckDispose(message)
CkPrivateKey::ckDispose(privateKey)
CkDkim::ckDispose(signer)
ProcedureReturn
EndProcedure