Java Requires Chilkat v11.0.0+
Chilkat2 Java
IMAP Download and Verify Signed (S/MIME) Email
See more IMAP Examples
Demonstrates how to download and verify digitally signed S/MIME email.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.MessageSet;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Imap imap = new Imap();
// Connect to an IMAP server.
// Use TLS
imap.setSsl(true);
imap.setPort(993);
try {
imap.connect("imap.example.com");
imap.login("myLogin", "myPassword");
// Select an IMAP mailbox
imap.selectMailbox("Inbox");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// We can choose to fetch UIDs or sequence numbers.
boolean fetchUids = true;
// Get the message IDs of all the emails in the mailbox
MessageSet messageSet = new MessageSet();
try {
imap.queryMbx("ALL", fetchUids, messageSet);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Email email = new Email();
Cert cert = new Cert();
int i = 0;
while (i < messageSet.getCount()) {
String uid = messageSet.getId(i);
System.out.println("uid: " + uid);
try {
imap.fetchEmail(false, uid, true, email);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// The security layers of signed and/or encrypted emails
// are automatically "unwrapped" when loaded into
// a Chilkat email object.
// An application only needs to check to see if an email
// was received signed or encrypted, and then examine
// the success/failure. For example:
if (email.getReceivedSigned()) {
System.out.println("This email was signed.");
// Check to see if the signatures were verified.
if (email.getSignaturesValid()) {
System.out.println("Digital signature(s) verified.");
System.out.println("Signer: " + email.getSignedBy());
// Get the certificate used for signing.
try {
email.lastSignerCert(0, cert);
System.out.println("Signing cert: " + cert.getSubjectCN());
} catch (ChilkatException e) {
System.out.println("Failed to get signing certificate object.");
}
} else {
System.out.println("Digital signature verification failed.");
}
}
i = i + 1;
}
// Disconnect from the IMAP server.
imap.disconnect();
}
}