DataFlex
DataFlex
IMAP Read Encrypted Email
See more IMAP Examples
Demonstrates 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.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoImap
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
Move False To iSuccess
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 = False) 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 = False) 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 = False) 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 Create (RefClass(cComChilkatMessageSet)) To hoMessageSet
If (Not(IsComObjectCreated(hoMessageSet))) Begin
Send CreateComObject of hoMessageSet
End
Get pvComObject of hoMessageSet to vMessageSet
Get ComQueryMbx Of hoImap "SUBJECT encrypted" True vMessageSet To iSuccess
If (iSuccess = 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."
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 Create (RefClass(cComChilkatEmail)) To hoEmail
If (Not(IsComObjectCreated(hoEmail))) Begin
Send CreateComObject of hoEmail
End
Get pvComObject of hoEmail to vEmail
Get ComFetchEmail Of hoImap False iUid True vEmail To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
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 Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
Get pvComObject of hoCert to vCert
Get ComLastDecryptCert Of hoEmail vCert To iSuccess
If (iSuccess <> False) Begin
Get ComSubjectDN Of hoCert To sTemp1
Showln "Certificate DN: " sTemp1
End
// Show the decrypted email body.
Get ComBody Of hoEmail To sTemp1
Showln sTemp1
Get ComDisconnect Of hoImap To iSuccess
End_Procedure