Sample code for 30+ languages & platforms
Java

Set an IMAP TLS Client Certificate from a PFX File

See more IMAP Examples

Demonstrates the Chilkat Imap.SetSslClientCertPfx method, which supplies a TLS client certificate and private key from a PKCS #12/PFX file. The first argument is the .pfx or .p12 file path and the second is its password. Call it before connecting.

Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.

Background: A PFX (PKCS #12) file bundles a certificate together with its private key in a single password-protected file — the format Windows and many corporate PKI tools export. This is the counterpart to SetSslClientCertPem: use it when your credentials are packaged as one .pfx rather than as separate PEM files.

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

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

    //  Demonstrates the Imap.SetSslClientCertPfx method, which supplies a TLS client certificate
    //  and private key from a PKCS #12/PFX file.  The 1st argument is the .pfx/.p12 file path and
    //  the 2nd is its password.  It must be called before connecting.

    CkImap imap = new CkImap();

    imap.put_Ssl(true);
    imap.put_Port(993);

    //  The PFX password is not hard-coded in source.  Insert code here to obtain it from an
    //  interactive prompt, environment variable, or a secrets vault.
    String pfxPassword;

    //  Provide the PFX client certificate BEFORE connecting.
    success = imap.SetSslClientCertPfx("qa_data/client.pfx",pfxPassword);
    if (success == false) {
        System.out.println(imap.lastErrorText());
        return;
        }

    success = imap.Connect("imap.example.com");
    if (success == false) {
        System.out.println(imap.lastErrorText());
        return;
        }

    success = imap.Login("user@example.com","myPassword");
    if (success == false) {
        System.out.println(imap.lastErrorText());
        return;
        }

    success = imap.Disconnect();
    if (success == false) {
        System.out.println(imap.lastErrorText());
        return;
        }
  }
}