Sample code for 30+ languages & platforms
Java

Send Encrypted Email using Certificate in Apple Keychain

See more Apple Keychain Examples

Send encrypted (S/MIME) email using a certificate found in the Apple Keychain.

Note: This example requires Chilkat v10.1.2 or later.

Chilkat Java Downloads

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

public class ChilkatExample {

    public static void main(String[] args) {
        Email email = new Email();
        email.setSubject("This email is encrypted");
        email.setBody("This is a S/MIME encrypted mail");
        email.setFrom("Joe <joe@example.com>");

        // Emails are encrypted using the recipient's certificate.
        String recipientEmailAddr = "jane@example2.com";
        email.addTo("", recipientEmailAddr);

        // Indicate that the email is to be sent encrypted.
        email.setSendEncrypted(true);

        Cert cert = new Cert();

        // -----------------------------------------------
        // This example requires Chilkat v10.1.2 or later.
        // -----------------------------------------------

        // The recipient's certificate is used to encrypt.
        // This will load the certificate from the Apple Keychain.
        try {
            cert.loadByEmailAddress(recipientEmailAddr);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Specify the certificate to be used for encryption.
        email.setEncryptCert(cert);
        email.setSendEncrypted(true);

        MailMan mailman = new MailMan();
        mailman.setSmtpHost("smtp.example.com");
        mailman.setSmtpUsername("joe@example.com");
        mailman.setSmtpPort(587);
        mailman.setStartTLS(true);

        // We'll get the SMTP password from the Apple Keychain.
        // See how we originally saved the email credentials to the Keychain here:
        // Save Email Credentials in Apple Keychain or Windows Credentials Manager
        Secrets secrets = new Secrets();

        // On Windows, this is the Windows Credentials Manager
        // On MacOS/iOS, it is the Apple Keychain
        secrets.setLocation("local_manager");

        // Specify the name of the secret.
        // service and username are required.
        // appName and domain are optional.
        // Note: The values are arbitrary and can be anything you want.
        JsonObject json = new JsonObject();
        json.updateString("appName", "MyEmailApp");
        json.updateString("service", "SMTP");
        json.updateString("domain", "example.com");
        json.updateString("username", "joe@example.com");

        mailman.setSmtpPassword(secrets.getSecretStr(json));

        // Send the encrypted email.
        // The email is encrypted and sent within the SendEmail function.
        try {
            mailman.sendEmail(email);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Encrypted email sent!");
    }
}