PowerShell Requires Chilkat v11.0.0+
PowerShell
Verify each DKIM signature in email downloaded over IMAP
See more DKIM Examples
Download the first ten messages (or all messages in a smaller inbox) as complete MIME, including attachments, while preserving their unread flags. The verifier looks up the selector’s TXT record when it needs a key. Inspect VerifyInfo immediately after each attempt for the signing domain, selector, and any body-hash mismatch.
Verify each signature independently, in top-to-bottom order. A passing signature authenticates the signing domain’s responsibility for the signed content. Identifying the author and checking DMARC alignment with the visible From address are separate steps. A missing DNS key or unavailable resolver can also prevent verification. DKIM specification.
Chilkat PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
$imap = New-Object Chilkat.Imap
$imap.Ssl = $true
$imap.Port = 993
$imap.RequireSslCertVerify = $true
$imap.PeekMode = $true
$imap.AutoDownloadAttachments = $true
$success = $imap.Connect("imap.example.com")
if ($success -ne $true) {
$($imap.LastErrorText)
exit
}
$success = $imap.Login("IMAP_USERNAME","IMAP_PASSWORD")
if ($success -ne $true) {
$($imap.LastErrorText)
exit
}
$success = $imap.SelectMailbox("INBOX")
if ($success -ne $true) {
$($imap.LastErrorText)
exit
}
# Select the first ten sequence positions, then fetch by stable UID.
$messages = New-Object Chilkat.MessageSet
$success = $imap.QueryMbx("1:10",$true,$messages)
if ($success -ne $true) {
$($imap.LastErrorText)
exit
}
$i = 0
$verifier = New-Object Chilkat.Dkim
$message = New-Object Chilkat.BinData
while (($i -lt $messages.Count)) {
$uid = $messages.GetId($i)
$("Message UID " + $uid)
$success = $imap.FetchSingleBd($uid,$true,$message)
if ($success -ne $true) {
$($imap.LastErrorText)
}
else {
$count = $verifier.NumDkimSigs($message)
if ($count -eq 0) {
$("No DKIM signatures.")
}
$sig = 0
while (($sig -lt $count)) {
$success = $verifier.DkimVerify($sig,$message)
# Read diagnostics before the next verification replaces them.
$info = New-Object Chilkat.JsonObject
$haveInfo = $info.Load($verifier.VerifyInfo)
if ($success -eq $true) {
$("Signature " + $sig + " verified.")
}
else {
if (($haveInfo -eq $true) -and ($info.StringOfEquals("bodyHashVerified","no",$true) -eq $true)) {
$("Signature " + $sig + ": the body hash differs.")
}
else {
$("Signature " + $sig + " could not be verified.")
}
$($verifier.LastErrorText)
}
if (($haveInfo -eq $true) -and ($info.JsonTypeOf("domain") -eq 1)) {
$("Signing domain: " + $info.StringOf("domain") + " Selector: " + $info.StringOf("selector"))
}
$sig = $sig + 1
}
}
$i = $i + 1
}
$success = $imap.Disconnect()