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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

set message = Server.CreateObject("Chilkat.BinData")
success = message.LoadFile("message.eml")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read message.eml.") & "</pre>"
    Response.End
End If

'  Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
set privateKey = Server.CreateObject("Chilkat.PrivateKey")
success = privateKey.LoadAnyFormatFile("dkim-private.pem","")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( privateKey.LastErrorText) & "</pre>"
    Response.End
End If

set signer = Server.CreateObject("Chilkat.Dkim")
signer.DkimDomain = "example.com"
signer.DkimSelector = "mail2026"
signer.DkimAlg = "rsa-sha256"
success = signer.SetDkimPrivateKey(privateKey)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( signer.LastErrorText) & "</pre>"
    Response.End
End If

'  DkimCanon takes a single setting and applies it to both headers and body.
signer.DkimCanon = "relaxed"
signer.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.
signer.DkimBodyLengthCount = 0
success = signer.DkimSign(message)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( signer.LastErrorText) & "</pre>"
    Response.End
End If

success = message.WriteFile("signed.eml")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not write signed.eml.") & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Saved signed.eml.") & "</pre>"

%>
</body>
</html>