Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Mail
oleobject loo_Email
oleobject loo_Message
oleobject loo_PrivateKey
oleobject loo_Signer
string ls_Recipients

li_Success = 0

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

loo_Mail = create oleobject
li_rc = loo_Mail.ConnectToNewObject("Chilkat.MailMan")
if li_rc < 0 then
    destroy loo_Mail
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Mail.SmtpHost = "smtp.example.com"
loo_Mail.SmtpPort = 465
loo_Mail.SmtpSsl = 1
loo_Mail.RequireSslCertVerify = 1
loo_Mail.SmtpUsername = "SMTP_USERNAME"
loo_Mail.SmtpPassword = "SMTP_PASSWORD"
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

loo_Email.From = "Workshop team <updates@example.com>"
loo_Email.Subject = "Workshop registration"
loo_Email.Body = "Registration opens on Monday."
li_Success = loo_Email.AddTo("Newsletter editor","editor@example.net")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Mail
    destroy loo_Email
    return
end if

//  Render after all headers, recipients, and content are final.
loo_Message = create oleobject
li_rc = loo_Message.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Mail.RenderToMimeBd(loo_Email,loo_Message)
if li_Success <> 1 then
    Write-Debug loo_Mail.LastErrorText
    destroy loo_Mail
    destroy loo_Email
    destroy loo_Message
    return
end if

//  Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
loo_PrivateKey = create oleobject
li_rc = loo_PrivateKey.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_PrivateKey.LoadAnyFormatFile("dkim-private.pem","")
if li_Success <> 1 then
    Write-Debug loo_PrivateKey.LastErrorText
    destroy loo_Mail
    destroy loo_Email
    destroy loo_Message
    destroy loo_PrivateKey
    return
end if

loo_Signer = create oleobject
li_rc = loo_Signer.ConnectToNewObject("Chilkat.Dkim")

loo_Signer.DkimDomain = "example.com"
loo_Signer.DkimSelector = "mail2026"
loo_Signer.DkimAlg = "rsa-sha256"
li_Success = loo_Signer.SetDkimPrivateKey(loo_PrivateKey)
if li_Success <> 1 then
    Write-Debug loo_Signer.LastErrorText
    destroy loo_Mail
    destroy loo_Email
    destroy loo_Message
    destroy loo_PrivateKey
    destroy loo_Signer
    return
end if

li_Success = loo_Signer.DkimSign(loo_Message)
if li_Success <> 1 then
    Write-Debug loo_Signer.LastErrorText
    destroy loo_Mail
    destroy loo_Email
    destroy loo_Message
    destroy loo_PrivateKey
    destroy loo_Signer
    return
end if

//  archive@example.com is an envelope-only blind copy.
ls_Recipients = "editor@example.net,archive@example.com"
li_Success = loo_Mail.SendMimeBd("updates@example.com",ls_Recipients,loo_Message)
if li_Success <> 1 then
    Write-Debug loo_Mail.LastErrorText
    destroy loo_Mail
    destroy loo_Email
    destroy loo_Message
    destroy loo_PrivateKey
    destroy loo_Signer
    return
end if

Write-Debug "SMTP relay accepted the signed message."
li_Success = loo_Mail.CloseSmtpConnection()
if li_Success <> 1 then
    Write-Debug "The SMTP session did not close cleanly."
end if



destroy loo_Mail
destroy loo_Email
destroy loo_Message
destroy loo_PrivateKey
destroy loo_Signer