![]() |
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
(Node.js) 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.
var os = require('os'); if (os.platform() == 'win32') { var chilkat = require('@chilkat/ck-node23-win64'); } else if (os.platform() == 'linux') { if (os.arch() == 'arm') { var chilkat = require('@chilkat/ck-node23-linux-arm'); } else if (os.arch() == 'arm64') { var chilkat = require('@chilkat/ck-node23-linux-arm64'); } else { var chilkat = require('@chilkat/ck-node23-linux-x64'); } } else if (os.platform() == 'darwin') { var chilkat = require('@chilkat/ck-node23-mac-universal'); } function chilkatExample() { var success = false; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. var imap = new chilkat.Imap(); // Connect to an IMAP server. // Use TLS imap.Ssl = true; imap.Port = 993; success = imap.Connect("imap.example.com"); if (success == false) { console.log(imap.LastErrorText); return; } success = imap.Login("myLogin","myPassword"); if (success == false) { console.log(imap.LastErrorText); return; } // Select an IMAP mailbox success = imap.SelectMailbox("Inbox"); if (success == false) { console.log(imap.LastErrorText); return; } // Download the 1st email (as MIME) in the Inbox by sequence number. var sbMime = new chilkat.StringBuilder(); success = imap.FetchSingleAsMimeSb(1,false,sbMime); if (success == false) { console.log(imap.LastErrorText); return; } // Load it into a MIME object and check to see if it is signed var mime = new chilkat.Mime(); mime.LoadMimeSb(sbMime); var alreadySavedParts = false; if (mime.ContainsSignedParts() == true) { // This will save the .p7s and other parts... var st = new chilkat.StringTable(); success = mime.PartsToFiles("c:/temp/qa_output",st); if (success == true) { var numFiles = st.Count; var i = 0; while (i < numFiles) { console.log("Created: " + st.StringAt(i)); i = i+1; } alreadySavedParts = true; } } // Load the MIME into an Email object. This unwraps the security layers and verifies signatures. var email = new chilkat.Email(); email.SetFromMimeSb(sbMime); if (email.ReceivedSigned == true) { console.log("This email was signed."); // Check to see if the signatures were verified. if (email.SignaturesValid == true) { console.log("Digital signature(s) verified."); console.log("Signer: " + email.SignedBy); // The certificate used for signing may be obtained // by calling email.GetSignedByCert. var cert = new chilkat.Cert(); success = email.LastSignerCert(i,cert); if (success == false) { console.log("Failed to get signing certificate object."); } else { console.log("Signing cert: " + cert.SubjectCN); } } } else { console.log("Digital signature verification failed."); } if (alreadySavedParts !== true) { email.SaveAllAttachments("c:/temp/qa_output"); } } chilkatExample(); |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.