Java
Chilkat2 Java
Set Cert and Private Key for S/MIME Decryption
See more IMAP Examples
Demonstrates the Chilkat Imap.SetDecryptCert2 method, which specifies a certificate together with a separately supplied private key for decrypting S/MIME messages. The first argument is the Cert and the second is the PrivateKey. This example loads the certificate and key from separate files.
Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.
Background: Sometimes the certificate (the public part) and the private key are stored separately — a
.cer alongside a PEM key file, for instance. This method pairs them explicitly, whereas SetDecryptCert expects a single Cert that already provides its private key. After the pairing, subsequently downloaded S/MIME messages are decrypted automatically.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.PrivateKey;
public class ChilkatExample {
public static void main(String[] args) {
// Demonstrates the Imap.SetDecryptCert2 method, which specifies a certificate together with a
// separately supplied private key for decrypting S/MIME messages. The 1st argument is the
// Cert and the 2nd is the PrivateKey.
Imap imap = new Imap();
imap.setSsl(true);
imap.setPort(993);
try {
imap.connect("imap.example.com");
imap.login("user@example.com", "myPassword");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Load the certificate and its private key from separate files.
Cert cert = new Cert();
try {
cert.loadFromFile("qa_data/smime.cer");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
PrivateKey privKey = new PrivateKey();
try {
privKey.loadPemFile("qa_data/smime_privkey.pem");
// Use the cert and key to automatically decrypt subsequently downloaded S/MIME messages.
imap.setDecryptCert2(cert, privKey);
imap.disconnect();
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
}
}