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

POP3 Verify Signed (S/MIME) Email

Demonstrates how to download and verify digitally signed S/MIME email.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.MailMan;
import com.chilkatsoft.chilkat2.StringTable;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        MailMan mailman = new MailMan();

        // Set the POP3 server's hostname
        mailman.setMailHost("pop.example.com");

        // Set the POP3 login/password.
        mailman.setPopUsername("myLogin");
        mailman.setPopPassword("myPassword");

        StringTable stUidls = new StringTable();
        try {
            mailman.fetchUidls(stUidls);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Email email = new Email();
        Cert cert = new Cert();

        int count = stUidls.getCount();
        int i = 0;
        while (i < count) {
            // Download the full email.
            try {
                mailman.fetchByUidl(stUidls.stringAt(i), false, 0, email);
            } catch (ChilkatException e) {
                System.out.println(e.getLastErrorText());
                return;
            }

            System.out.println(i);
            System.out.println("From: " + email.getFrom());
            System.out.println("Subject: " + email.getSubject());

            // The security layers of signed and/or encrypted emails
            // are automatically "unwrapped" when loaded into
            // a Chilkat email object.
            // An application only needs to check to see if an email
            // was received signed or encrypted, and then examine
            // the success/failure.  For example:
            if (email.getReceivedSigned()) {
                System.out.println("This email was signed.");

                // Check to see if the signatures were verified.
                if (email.getSignaturesValid()) {
                    System.out.println("Digital signature(s) verified.");
                    System.out.println("Signer: " + email.getSignedBy());

                    try {
                        email.lastSignerCert(0, cert);
                    } catch (ChilkatException e) {
                        System.out.println(e.getLastErrorText());
                        return;
                    }

                    System.out.println("Signing cert: " + cert.getSubjectCN());
                }
            } else {
                System.out.println("Digital signature verification failed.");
            }

            i = i + 1;
        }

        mailman.pop3EndSession();
    }
}