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

Using Client Certificate w/ IMAP SSL

Demonstrates how to use a client-side certificate with an IMAP SSL connection. The SetSslClientCert method is called to specify a certificate to be used for the SSL connection.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.CertStore;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.EmailBundle;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.MessageSet;

public class ChilkatExample {

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

        Imap imap = new Imap();

        // To use a secure SSL connection, set SSL and the port:
        imap.setSsl(true);
        // The typical port for IMAP SSL is 993
        imap.setPort(993);

        // Load a certificate from a PFX file and use it.
        // Note: Other methods are available to load pre-installed
        // certificates from registry-based certificate stores.

        // Create an instance of a certificate store object, load a PFX file,
        // locate the certificate we need, and use it for signing.
        // (a PFX file may contain more than one certificate.)
        CertStore certStore = new CertStore();
        // The 1st argument is the filename, the 2nd arg is the
        // PFX file's password:
        try {
            certStore.loadPfxFile("myCertWithPrivateKey.pfx", "secret");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Find the certificate by the subject common name:
        JsonObject jsonCN = new JsonObject();
        jsonCN.updateString("CN", "cert common name");

        Cert cert = new Cert();
        try {
            certStore.findCert(jsonCN, cert);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // If a PFX file is known to contain a single certificate,
        // you may load it directly into a Chilkat certificate object.
        // This snippet of source code shows how:
        Cert cert2 = new Cert();
        // The 1st argument is the filename, the 2nd arg is the
        // PFX file's password:
        try {
            cert2.loadPfxFile("myClientCert.pfx", "secret");
        } catch (ChilkatException e) {
            System.out.println(cert.getLastErrorText());
            return;
        }

        // Use the cert:
        imap.setSslClientCert(cert);

        // Connect to an IMAP server.
        try {
            imap.connect("imap.example.com");

            // Login
            imap.login("myLogin", "myPassword");

            // Select an IMAP mailbox
            imap.selectMailbox("Inbox");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Get the message IDs of all the emails in the mailbox
        boolean fetchUids = true;
        MessageSet messageSet = new MessageSet();
        try {
            imap.queryMbx("ALL", fetchUids, messageSet);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Fetch the emails into a bundle object:
        EmailBundle bundle = new EmailBundle();
        boolean headersOnly = false;
        try {
            imap.fetchMsgSet(headersOnly, messageSet, bundle);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Loop over the bundle and display the FROM and SUBJECT of each.
        Email email = new Email();
        int i = 0;
        int numEmails = bundle.getMessageCount();
        while (i < numEmails) {
            bundle.emailAt(i, email);

            System.out.println(email.getFrom());
            System.out.println(email.getSubject());
            System.out.println("--");
            i = i + 1;
        }

        // Disconnect from the IMAP server.
        imap.disconnect();
    }
}