Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(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.

Note: This example requires Chilkat v10.1.2 or later because it uses the Secrets class.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

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.