![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Java) IMAP Download and Verify Signed MIMEDownloads 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. Note: This example requires Chilkat v11.0.0 or greater.
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; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkImap imap = new CkImap(); // Connect to an IMAP server. // Use TLS imap.put_Ssl(true); imap.put_Port(993); success = imap.Connect("imap.example.com"); if (success == false) { System.out.println(imap.lastErrorText()); return; } success = imap.Login("myLogin","myPassword"); if (success == false) { System.out.println(imap.lastErrorText()); return; } // Select an IMAP mailbox success = imap.SelectMailbox("Inbox"); if (success == false) { System.out.println(imap.lastErrorText()); return; } // Download the 1st email (as MIME) in the Inbox by sequence number. CkStringBuilder sbMime = new CkStringBuilder(); success = imap.FetchSingleAsMimeSb(1,false,sbMime); if (success == false) { System.out.println(imap.lastErrorText()); return; } // Load it into a MIME object and check to see if it is signed CkMime mime = new CkMime(); mime.LoadMimeSb(sbMime); boolean alreadySavedParts = false; if (mime.ContainsSignedParts() == true) { // This will save the .p7s and other parts... CkStringTable st = new CkStringTable(); success = mime.PartsToFiles("c:/temp/qa_output",st); if (success == true) { int numFiles = st.get_Count(); 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. CkEmail email = new CkEmail(); email.SetFromMimeSb(sbMime); if (email.get_ReceivedSigned() == true) { System.out.println("This email was signed."); // Check to see if the signatures were verified. if (email.get_SignaturesValid() == true) { System.out.println("Digital signature(s) verified."); System.out.println("Signer: " + email.signedBy()); // The certificate used for signing may be obtained // by calling email.GetSignedByCert. CkCert cert = new CkCert(); success = email.LastSignerCert(i,cert); if (success == false) { System.out.println("Failed to get signing certificate object."); } else { System.out.println("Signing cert: " + cert.subjectCN()); } } } else { System.out.println("Digital signature verification failed."); } if (alreadySavedParts != true) { email.SaveAllAttachments("c:/temp/qa_output"); } } } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.