Sample code for 30+ languages & platforms
Java

Use a PEM TLS Client Certificate

See more SMTP Examples

Demonstrates the Chilkat MailMan.SetSslClientCertPem method, which sets the client-side certificate for SSL/TLS connections, loading it from PEM data or from a PEM file. The first argument may contain the PEM text itself or a path to a PEM file; the second is the PEM password. This example loads the client certificate from a PEM file.

Background: PEM is the familiar Base64 text format bracketed by -----BEGIN CERTIFICATE----- lines, and a single PEM can hold both a certificate and its (optionally encrypted) private key. It is the common format in Unix/OpenSSL environments, whereas PFX is more typical on Windows. Because this method accepts either the PEM text or a filename, you can supply credentials straight from a config value or secret store without writing them to disk.

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 MailMan.SetSslClientCertPem method, which sets the client-side
    //  certificate for SSL/TLS connections, loading it from PEM data or from a PEM file.  The
    //  1st argument may contain PEM text or a PEM file path; the 2nd is the PEM password.

    CkMailMan mailman = new CkMailMan();

    //  Configure the SMTP server connection.
    mailman.put_SmtpHost("smtp.example.com");
    mailman.put_SmtpPort(465);
    mailman.put_SmtpSsl(true);
    mailman.put_SmtpUsername("user@example.com");
    mailman.put_SmtpPassword("myPassword");

    //  Load the client certificate from a PEM file.
    success = mailman.SetSslClientCertPem("qa_data/certs/client.pem","pem_password");
    if (success == false) {
        System.out.println(mailman.lastErrorText());
        return;
        }

    success = mailman.VerifySmtpLogin();
    if (success == false) {
        System.out.println(mailman.lastErrorText());
        return;
        }

    System.out.println("Connected using a PEM TLS client certificate.");

    //  Note: The path "qa_data/certs/client.pem" is a relative local filesystem path,
    //  relative to the current working directory of the running application.
  }
}