Xbase++
Xbase++
Verify DKIM-Signature Headers in Downloaded Email
See more DKIM / DomainKey Examples
Downloads email from an IMAP server and verifies the DKIM-Signature header(s) in each email, if present.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oImap
LOCAL oDkim
LOCAL nBUid
LOCAL nSeqNum
LOCAL j
LOCAL n
LOCAL oJson
LOCAL oMimeData
LOCAL nNumDkim
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oImap := CreateObject("Chilkat.Imap")
// Connect to an IMAP server, login, select mailbox..
// Use TLS
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 1)
nSuccess := oImap:Login("myLogin", "myPassword")
IF (nSuccess == 1)
nSuccess := oImap:SelectMailbox("Inbox")
ENDIF
ENDIF
IF (nSuccess != 1)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
oDkim := CreateObject("Chilkat.Dkim")
// Download a max of 10 emails and verify any DKIM-Signature headers
// that are present.
// Download emails by sequence numbers (not UIDs).
nBUid := 0
n := oImap:NumMessages
IF (n > 10)
n := 10
ENDIF
oJson := CreateObject("Chilkat.JsonObject")
oJson:EmitCompact := 0
// To verify DKIM-Signature headers, we need the exact unmodified MIME bytes of each email.
oMimeData := CreateObject("Chilkat.BinData")
nSeqNum := 1
DO WHILE nSeqNum <= n
// The FetchSingleBd method was introduced in v9.5.0.76
nSuccess := oImap:FetchSingleBd(nSeqNum, nBUid, oMimeData)
IF (nSuccess != 1)
? oImap:LastErrorText
oImap:destroy()
oDkim:destroy()
oJson:destroy()
oMimeData:destroy()
RETURN
ENDIF
// Get the number of DKIM-Signature headers.
nNumDkim := oDkim:NumDkimSigs(oMimeData)
// Verify each..
j := 0
DO WHILE j < nNumDkim
? "------ DKIM Signature " + Str(j)
nSuccess := oDkim:DkimVerify(j, oMimeData)
IF (nSuccess != 1)
? "Not valid."
ELSE
? "valid."
ENDIF
// Show the additional information about the signature verification
oJson:Load(oDkim:VerifyInfo)
? oJson:Emit()
// The JSON contains information such as this:
// {
// "domain": "amazonses.com",
// "selector": "7v7vs6w47njt4pimodk5mmttbegzsi6n",
// "publicKey": "MIGfMA0GCSqG...v2GvWPqGHz6uqeQIDAQAB",
// "canonicalization": "relaxed/simple",
// "algorithm": "rsa-sha256",
// "signedHeaders": "Subject:From:To:Date:Mime-Version:Content-Type:References:Message-Id:Feedback-ID",
// "verified": "yes"
// }
j := j + 1
ENDDO
nSeqNum := nSeqNum + 1
ENDDO
nSuccess := oImap:Disconnect()
oImap:destroy()
oDkim:destroy()
oJson:destroy()
oMimeData:destroy()