CkJava Requires Chilkat v11.0.0+
CkJava
Create an encrypted S/MIME file without SMTP
See more Email Object Examples
Encrypt a saved draft for a recipient with an RSA encryption certificate. This example uses AES-256-GCM for the content and RSA-OAEP for the content-encryption key. Confirm these choices are supported by the receiving mail client. RenderToMimeBd produces the protected bytes without submitting mail.
Chilkat CkJava Downloads
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;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkEmail email = new CkEmail();
success = email.LoadEml("message.eml");
if (success != true) {
System.out.println(email.lastErrorText());
return;
}
// Use the recipient's public encryption certificate. Its private key stays with the recipient.
CkCert cert = new CkCert();
success = cert.LoadFromFile("recipient.cer");
if (success != true) {
System.out.println(cert.lastErrorText());
return;
}
success = email.SetEncryptCert(cert);
if (success != true) {
System.out.println(email.lastErrorText());
return;
}
email.put_Pkcs7CryptAlg("aes-gcm");
email.put_Pkcs7KeyLength(256);
email.put_OaepPadding(true);
email.put_OaepHash("sha256");
email.put_OaepMgfHash("sha256");
email.put_SendEncrypted(true);
CkMailMan mail = new CkMailMan();
CkBinData rendered = new CkBinData();
success = mail.RenderToMimeBd(email,rendered);
if (success != true) {
System.out.println(mail.lastErrorText());
return;
}
success = rendered.WriteFile("encrypted.eml");
if (success != true) {
System.out.println("Failed: rendered.WriteFile.");
return;
}
System.out.println("Saved encrypted.eml.");
}
}