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
(DataFlex) IMAP Read Encrypted EmailSee more IMAP ExamplesDemonstrates 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. Note: This example requires Chilkat v10.1.2 or later because it uses the Secrets class.
Use ChilkatAx-win32.pkg Procedure Test Handle hoImap Boolean iSuccess Handle hoSecrets Variant vJson Handle hoJson String sPassword Variant vMessageSet Handle hoMessageSet UInteger iUid Variant vEmail Handle hoEmail Variant vCert Handle hoCert String sTemp1 Integer iTemp1 Boolean bTemp1 Get Create (RefClass(cComChilkatImap)) To hoImap If (Not(IsComObjectCreated(hoImap))) Begin Send CreateComObject of hoImap End Set ComSsl Of hoImap To True Set ComPort Of hoImap To 993 Get ComConnect Of hoImap "imap.example2.com" To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoImap To sTemp1 Showln sTemp1 Procedure_Return End // 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 Get Create (RefClass(cComChilkatSecrets)) To hoSecrets If (Not(IsComObjectCreated(hoSecrets))) Begin Send CreateComObject of hoSecrets End // On Windows, this is the Windows Credentials Manager // On MacOS/iOS, it is the Apple Keychain Set ComLocation Of hoSecrets To "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. Get Create (RefClass(cComChilkatJsonObject)) To hoJson If (Not(IsComObjectCreated(hoJson))) Begin Send CreateComObject of hoJson End Get ComUpdateString Of hoJson "appName" "MyEmailApp" To iSuccess Get ComUpdateString Of hoJson "service" "IMAP" To iSuccess Get ComUpdateString Of hoJson "domain" "example2.com" To iSuccess Get ComUpdateString Of hoJson "username" "jane@example2.com" To iSuccess Get pvComObject of hoJson to vJson Get ComGetSecretStr Of hoSecrets vJson To sPassword Get ComLastMethodSuccess Of hoSecrets To bTemp1 If (bTemp1 = False) Begin Get ComLastErrorText Of hoSecrets To sTemp1 Showln sTemp1 Procedure_Return End Get ComLogin Of hoImap "jane@example2.com" sPassword To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoImap To sTemp1 Showln sTemp1 Procedure_Return End // Select an IMAP mailbox Get ComSelectMailbox Of hoImap "Inbox" To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoImap To sTemp1 Showln sTemp1 Procedure_Return End // 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. Get ComSearch Of hoImap "SUBJECT encrypted" True To vMessageSet If (IsComObject(vMessageSet)) Begin Get Create (RefClass(cComChilkatMessageSet)) To hoMessageSet Set pvComObject Of hoMessageSet To vMessageSet End Get ComLastMethodSuccess Of hoImap To bTemp1 If (bTemp1 = False) Begin Get ComLastErrorText Of hoImap To sTemp1 Showln sTemp1 Procedure_Return End Get ComCount Of hoMessageSet To iTemp1 If (iTemp1 = 0) Begin Showln "No messages found." Send Destroy of hoMessageSet Procedure_Return End // 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. Get ComGetId Of hoMessageSet 0 To iUid Get ComFetchSingle Of hoImap iUid True To vEmail If (IsComObject(vEmail)) Begin Get Create (RefClass(cComChilkatEmail)) To hoEmail Set pvComObject Of hoEmail To vEmail End Get ComLastMethodSuccess Of hoImap To bTemp1 If (bTemp1 = False) Begin Get ComLastErrorText Of hoImap To sTemp1 Showln sTemp1 Send Destroy of hoMessageSet Procedure_Return End // Here we can show if the email was received encrypted, if it was successfully decrypted, and // which certificate was used to decrypt. Get ComReceivedEncrypted Of hoEmail To bTemp1 Showln "Email received encrypted: " bTemp1 // Was it successfully decrypted? Get ComDecrypted Of hoEmail To bTemp1 Showln "Successfully decrypted: " bTemp1 // What cert was used to decrypt? Get ComEncryptedBy Of hoEmail To sTemp1 Showln "Encrypted by: " sTemp1 Get ComGetEncryptedByCert Of hoEmail To vCert If (IsComObject(vCert)) Begin Get Create (RefClass(cComChilkatCert)) To hoCert Set pvComObject Of hoCert To vCert End Get ComLastMethodSuccess Of hoEmail To bTemp1 If (bTemp1 = True) Begin Get ComSubjectDN Of hoCert To sTemp1 Showln "Certificate DN: " sTemp1 Send Destroy of hoCert End // Show the decrypted email body. Get ComBody Of hoEmail To sTemp1 Showln sTemp1 Send Destroy of hoMessageSet Send Destroy of hoEmail Get ComDisconnect Of hoImap To iSuccess End_Procedure |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.