Java Requires Chilkat v11.0.0+
Chilkat2 Java
IMAP Download and Verify Signed MIME
See more IMAP Examples
Downloads the original MIME of a digitally signed email and saves the .p7s signature along with other MIME parts. It then imports the email into a Chilkat email object to unwrap the S/MIME and verify the signature, and subsequently saves the attachments if they haven't been saved already.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.Mime;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.StringTable;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// 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;
}
// Download the 1st email (as MIME) in the Inbox by sequence number.
StringBuilder sbMime = new StringBuilder();
try {
imap.fetchSingleAsMimeSb(1, false, sbMime);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Load it into a MIME object and check to see if it is signed
Mime mime = new Mime();
mime.loadMimeSb(sbMime);
boolean alreadySavedParts = false;
if (mime.containsSignedParts()) {
// This will save the .p7s and other parts...
StringTable st = new StringTable();
try {
mime.partsToFiles("c:/temp/qa_output", st);
success = true;
} catch (ChilkatException e) {
success = false;
}
if (success) {
int numFiles = st.getCount();
int i = 0;
while (i < numFiles) {
System.out.println("Created: " + st.stringAt(i));
i = i + 1;
}
alreadySavedParts = true;
}
}
// Load the MIME into an Email object. This unwraps the security layers and verifies signatures.
Email email = new Email();
email.setFromMimeSb(sbMime);
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());
// The certificate used for signing may be obtained
// by calling email.LastSignerCert. Index 0 is the first signer.
Cert cert = new Cert();
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.");
}
} else {
System.out.println("This email was not signed.");
}
if (!alreadySavedParts) {
email.saveAllAttachments("c:/temp/qa_output");
}
}
}