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
(PureBasic) 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.
IncludeFile "CkJsonObject.pb" IncludeFile "CkMessageSet.pb" IncludeFile "CkImap.pb" IncludeFile "CkEmail.pb" IncludeFile "CkSecrets.pb" IncludeFile "CkCert.pb" Procedure ChilkatExample() imap.i = CkImap::ckCreate() If imap.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkImap::setCkSsl(imap, 1) CkImap::setCkPort(imap, 993) success.i = CkImap::ckConnect(imap,"imap.example2.com") If success <> 1 Debug CkImap::ckLastErrorText(imap) CkImap::ckDispose(imap) ProcedureReturn EndIf ; 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 secrets.i = CkSecrets::ckCreate() If secrets.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; On Windows, this is the Windows Credentials Manager ; On MacOS/iOS, it is the Apple Keychain CkSecrets::setCkLocation(secrets, "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. json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckUpdateString(json,"appName","MyEmailApp") CkJsonObject::ckUpdateString(json,"service","IMAP") CkJsonObject::ckUpdateString(json,"domain","example2.com") CkJsonObject::ckUpdateString(json,"username","jane@example2.com") password.s = CkSecrets::ckGetSecretStr(secrets,json) If CkSecrets::ckLastMethodSuccess(secrets) = 0 Debug CkSecrets::ckLastErrorText(secrets) CkImap::ckDispose(imap) CkSecrets::ckDispose(secrets) CkJsonObject::ckDispose(json) ProcedureReturn EndIf success = CkImap::ckLogin(imap,"jane@example2.com",password) If success <> 1 Debug CkImap::ckLastErrorText(imap) CkImap::ckDispose(imap) CkSecrets::ckDispose(secrets) CkJsonObject::ckDispose(json) ProcedureReturn EndIf ; Select an IMAP mailbox success = CkImap::ckSelectMailbox(imap,"Inbox") If success <> 1 Debug CkImap::ckLastErrorText(imap) CkImap::ckDispose(imap) CkSecrets::ckDispose(secrets) CkJsonObject::ckDispose(json) ProcedureReturn EndIf ; 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. messageSet.i = CkImap::ckSearch(imap,"SUBJECT encrypted",1) If CkImap::ckLastMethodSuccess(imap) = 0 Debug CkImap::ckLastErrorText(imap) CkImap::ckDispose(imap) CkSecrets::ckDispose(secrets) CkJsonObject::ckDispose(json) ProcedureReturn EndIf If CkMessageSet::ckCount(messageSet) = 0 Debug "No messages found." CkMessageSet::ckDispose(messageSet) CkImap::ckDispose(imap) CkSecrets::ckDispose(secrets) CkJsonObject::ckDispose(json) ProcedureReturn EndIf ; 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. uid.i = CkMessageSet::ckGetId(messageSet,0) email.i = CkImap::ckFetchSingle(imap,uid,1) If CkImap::ckLastMethodSuccess(imap) = 0 Debug CkImap::ckLastErrorText(imap) CkMessageSet::ckDispose(messageSet) CkImap::ckDispose(imap) CkSecrets::ckDispose(secrets) CkJsonObject::ckDispose(json) ProcedureReturn EndIf ; Here we can show if the email was received encrypted, if it was successfully decrypted, and ; which certificate was used to decrypt. Debug "Email received encrypted: " + Str(CkEmail::ckReceivedEncrypted(email)) ; Was it successfully decrypted? Debug "Successfully decrypted: " + Str(CkEmail::ckDecrypted(email)) ; What cert was used to decrypt? Debug "Encrypted by: " + CkEmail::ckEncryptedBy(email) cert.i = CkEmail::ckGetEncryptedByCert(email) If CkEmail::ckLastMethodSuccess(email) = 1 Debug "Certificate DN: " + CkCert::ckSubjectDN(cert) CkCert::ckDispose(cert) EndIf ; Show the decrypted email body. Debug CkEmail::ckBody(email) CkMessageSet::ckDispose(messageSet) CkEmail::ckDispose(email) CkImap::ckDisconnect(imap) CkImap::ckDispose(imap) CkSecrets::ckDispose(secrets) CkJsonObject::ckDispose(json) ProcedureReturn EndProcedure |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.