Sample code for 30+ languages & platforms
Xbase++

Verify DomainKey-Signature Headers in Downloaded Email

See more DKIM / DomainKey Examples

Downloads email from an IMAP server and verifies the DomainKey-Signature header(s) in each email, if present.

Note: DKIM-Signatures are much more common than DomainKey-Signatures. See the other Chilkat example for verifying DKIM-Signatures (link in the code below).

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oDkim
LOCAL nBUid
LOCAL nSeqNum
LOCAL j
LOCAL n
LOCAL oJson
LOCAL oMimeData
LOCAL nNumSigs

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

//  Note: DKIM-Signatures are much more common than DomainKey-Signature
//  See DKIM-Signature Verify Sample.

oDkim := CreateObject("Chilkat.Dkim")

//  Download a max of 10 emails and verify any DomainKey-Signature headers
//  that are present.

//  Download emails by sequence numbers (not UIDs).
nBUid := 0

n := oImap:NumMessages
IF (n > 50)
    n := 50
ENDIF

oJson := CreateObject("Chilkat.JsonObject")
oJson:EmitCompact := 0

//  To verify DomainKey-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

    //  Note: DKIM-Signatures are much more common than DomainKey-Signature
    //  See DKIM-Signature Verify Sample.

    //  Get the number of DomainKey-Signature headers.
    nNumSigs := oDkim:NumDomainKeySigs(oMimeData)

    //  Verify each..
    j := 0
    DO WHILE j < nNumSigs
        ? "------ DomainKey Signature " + Str(j)

        nSuccess := oDkim:DomainKeyVerify(j, oMimeData)
        IF (nSuccess != 1)
            ? "Not valid."
            ? oDkim:LastErrorText
        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()