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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMail
    Variant vEmail
    Handle hoEmail
    Variant vMessage
    Handle hoMessage
    Variant vPrivateKey
    Handle hoPrivateKey
    Handle hoSigner
    String sRecipients
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatMailMan)) To hoMail
    If (Not(IsComObjectCreated(hoMail))) Begin
        Send CreateComObject of hoMail
    End
    Set ComSmtpHost Of hoMail To "smtp.example.com"
    Set ComSmtpPort Of hoMail To 465
    Set ComSmtpSsl Of hoMail To True
    Set ComRequireSslCertVerify Of hoMail To True
    Set ComSmtpUsername Of hoMail To "SMTP_USERNAME"
    Set ComSmtpPassword Of hoMail To "SMTP_PASSWORD"
    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComFrom Of hoEmail To "Workshop team <updates@example.com>"
    Set ComSubject Of hoEmail To "Workshop registration"
    Set ComBody Of hoEmail To "Registration opens on Monday."
    Get ComAddTo Of hoEmail "Newsletter editor" "editor@example.net" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Render after all headers, recipients, and content are final.
    Get Create (RefClass(cComChilkatBinData)) To hoMessage
    If (Not(IsComObjectCreated(hoMessage))) Begin
        Send CreateComObject of hoMessage
    End
    Get pvComObject of hoEmail to vEmail
    Get pvComObject of hoMessage to vMessage
    Get ComRenderToMimeBd Of hoMail vEmail vMessage To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivateKey
    If (Not(IsComObjectCreated(hoPrivateKey))) Begin
        Send CreateComObject of hoPrivateKey
    End
    Get ComLoadAnyFormatFile Of hoPrivateKey "dkim-private.pem" "" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoPrivateKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatDkim)) To hoSigner
    If (Not(IsComObjectCreated(hoSigner))) Begin
        Send CreateComObject of hoSigner
    End
    Set ComDkimDomain Of hoSigner To "example.com"
    Set ComDkimSelector Of hoSigner To "mail2026"
    Set ComDkimAlg Of hoSigner To "rsa-sha256"
    Get pvComObject of hoPrivateKey to vPrivateKey
    Get ComSetDkimPrivateKey Of hoSigner vPrivateKey To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoSigner To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoMessage to vMessage
    Get ComDkimSign Of hoSigner vMessage To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoSigner To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  archive@example.com is an envelope-only blind copy.
    Move "editor@example.net,archive@example.com" To sRecipients
    Get pvComObject of hoMessage to vMessage
    Get ComSendMimeBd Of hoMail "updates@example.com" sRecipients vMessage To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoMail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "SMTP relay accepted the signed message."
    Get ComCloseSmtpConnection Of hoMail To iSuccess
    If (iSuccess <> True) Begin
        Showln "The SMTP session did not close cleanly."
    End



End_Procedure