Java Requires Chilkat v11.0.0+
Chilkat2 Java
IMAP Read Encrypted Email
See more IMAP Examples
Demonstrates how to read encrypted email from an IMAP mailbox.Reading encrypted email works the same as reading non-encrypted email. If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.
Information about the original encrypted state of the email is available after it has been downloaded and decrypted.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.MessageSet;
import com.chilkatsoft.chilkat2.Secrets;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
Imap imap = new Imap();
imap.setSsl(true);
imap.setPort(993);
try {
imap.connect("imap.example2.com");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// We'll get the IMAP email account's password from the Apple Keychain or Windows Credentials Manager.
// 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", "IMAP");
json.updateString("domain", "example2.com");
json.updateString("username", "jane@example2.com");
String password;
try {
password = secrets.getSecretStr(json);
imap.login("jane@example2.com", password);
// Select an IMAP mailbox
imap.selectMailbox("Inbox");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// This example: Send Encrypted Email using Certificate in Apple Keychain
// sent an email with the subject "This email is encrypted".
// Let's download an email with the word "encrypted" in the subject.
MessageSet messageSet = new MessageSet();
try {
imap.queryMbx("SUBJECT encrypted", true, messageSet);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
if (messageSet.getCount() == 0) {
System.out.println("No messages found.");
return;
}
// Reading encrypted email works the same as reading non-encrypted email.
// If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store),
// Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.
long uid = messageSet.getId(0);
Email email = new Email();
try {
imap.fetchEmail(false, uid, true, email);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Here we can show if the email was received encrypted, if it was successfully decrypted, and
// which certificate was used to decrypt.
System.out.println("Email received encrypted: " + email.getReceivedEncrypted());
// Was it successfully decrypted?
System.out.println("Successfully decrypted: " + email.getDecrypted());
// What cert was used to decrypt?
System.out.println("Encrypted by: " + email.getEncryptedBy());
Cert cert = new Cert();
try {
email.lastDecryptCert(cert);
success = true;
} catch (ChilkatException e) {
success = false;
}
if (success) {
System.out.println("Certificate DN: " + cert.getSubjectDN());
}
// Show the decrypted email body.
System.out.println(email.getBody());
imap.disconnect();
}
}