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

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 Java Downloads

Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Dkim;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.MessageSet;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // Assumes Chilkat is unlocked. See Global Unlock Sample.

        Imap imap = new Imap();
        imap.setSsl(true);
        imap.setPort(993);
        imap.setRequireSslCertVerify(true);
        imap.setPeekMode(true);
        imap.setAutoDownloadAttachments(true);
        try {
            imap.connect("imap.example.com");

            imap.login("IMAP_USERNAME", "IMAP_PASSWORD");

            imap.selectMailbox("INBOX");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Select the first ten sequence positions, then fetch by stable UID.
        MessageSet messages = new MessageSet();
        try {
            imap.queryMbx("1:10", true, messages);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        int i = 0;
        Dkim verifier = new Dkim();
        BinData message = new BinData();
        while (i < messages.getCount()) {
            long uid = messages.getId(i);
            System.out.println("Message UID " + uid);
            try {
                imap.fetchSingleBd(uid, true, message);
                success = true;
            } catch (ChilkatException e) {
                success = false;
            }
            if (!success) {
                System.out.println(imap.getLastErrorText());
            } else {
                int count = verifier.numDkimSigs(message);
                if (count == 0) {
                    System.out.println("No DKIM signatures.");
                }

                int sig = 0;
                while (sig < count) {
                    try {
                        verifier.dkimVerify(sig, message);
                        success = true;
                    } catch (ChilkatException e) {
                        success = false;
                    }
                    // Read diagnostics before the next verification replaces them.
                    JsonObject info = new JsonObject();
                    boolean haveInfo;
                    try {
                        info.load(verifier.getVerifyInfo());
                        haveInfo = true;
                    } catch (ChilkatException e) {
                        haveInfo = false;
                    }
                    if (success) {
                        System.out.println("Signature " + sig + " verified.");
                    } else {
                        if ((haveInfo) && (info.stringOfEquals("bodyHashVerified", "no", true))) {
                            System.out.println("Signature " + sig + ": the body hash differs.");
                        } else {
                            System.out.println("Signature " + sig + " could not be verified.");
                        }

                        System.out.println(verifier.getLastErrorText());
                    }

                    if ((haveInfo) && (info.jsonTypeOf("domain") == 1)) {
                        System.out.println("Signing domain: " + info.stringOf("domain") + "  Selector: " + info.stringOf("selector"));
                    }

                    sig = sig + 1;
                }
            }

            i = i + 1;
        }

        imap.disconnect();
    }
}