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

Sign a saved MIME message with DKIM

See more DKIM Examples

Sign the complete bytes of message.eml and save the result as signed.eml. Finish the message’s From, To, Date, Message-ID, MIME parts and transfer encodings before signing. Existing signatures stay in place and the new signature is inserted first.

The example signs the full body and includes From in the signed headers. Relaxed canonicalization tolerates defined whitespace and header folding changes. Editing a signed field or changing the body text can still break verification. Prepare the matching DNS record.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loMessage
LOCAL loPrivateKey
LOCAL loSigner

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loMessage = CreateObject('Chilkat.BinData')
lnSuccess = loMessage.LoadFile("message.eml")
IF (lnSuccess <> 1) THEN
    ? "Could not read message.eml."
    RELEASE loMessage
    CANCEL
ENDIF

*  Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
loPrivateKey = CreateObject('Chilkat.PrivateKey')
lnSuccess = loPrivateKey.LoadAnyFormatFile("dkim-private.pem","")
IF (lnSuccess <> 1) THEN
    ? loPrivateKey.LastErrorText
    RELEASE loMessage
    RELEASE loPrivateKey
    CANCEL
ENDIF

loSigner = CreateObject('Chilkat.Dkim')
loSigner.DkimDomain = "example.com"
loSigner.DkimSelector = "mail2026"
loSigner.DkimAlg = "rsa-sha256"
lnSuccess = loSigner.SetDkimPrivateKey(loPrivateKey)
IF (lnSuccess <> 1) THEN
    ? loSigner.LastErrorText
    RELEASE loMessage
    RELEASE loPrivateKey
    RELEASE loSigner
    CANCEL
ENDIF

*  DkimCanon takes a single setting and applies it to both headers and body.
loSigner.DkimCanon = "relaxed"
loSigner.DkimHeaders = "from:to:subject:date:message-id:mime-version:content-type:content-transfer-encoding"
*  Zero signs the entire canonicalized body and omits the optional l= tag.
loSigner.DkimBodyLengthCount = 0
lnSuccess = loSigner.DkimSign(loMessage)
IF (lnSuccess <> 1) THEN
    ? loSigner.LastErrorText
    RELEASE loMessage
    RELEASE loPrivateKey
    RELEASE loSigner
    CANCEL
ENDIF

lnSuccess = loMessage.WriteFile("signed.eml")
IF (lnSuccess <> 1) THEN
    ? "Could not write signed.eml."
    RELEASE loMessage
    RELEASE loPrivateKey
    RELEASE loSigner
    CANCEL
ENDIF

? "Saved signed.eml."

RELEASE loMessage
RELEASE loPrivateKey
RELEASE loSigner