Chilkat Examples

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

PowerBuilder 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

 

 

 

(PowerBuilder) 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

integer li_rc
oleobject loo_Imap
integer li_Success
oleobject loo_Secrets
oleobject loo_Json
string ls_Password
oleobject loo_MessageSet
integer li_Uid
oleobject loo_Email
oleobject loo_Cert

loo_Imap = create oleobject
// Use "Chilkat_9_5_0.Imap" for versions of Chilkat < 10.0.0
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Imap.Ssl = 1
loo_Imap.Port = 993
li_Success = loo_Imap.Connect("imap.example2.com")
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

// 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
loo_Secrets = create oleobject
// Use "Chilkat_9_5_0.Secrets" for versions of Chilkat < 10.0.0
li_rc = loo_Secrets.ConnectToNewObject("Chilkat.Secrets")

// On Windows, this is the Windows Credentials Manager
// On MacOS/iOS, it is the Apple Keychain
loo_Secrets.Location = "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.
loo_Json = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("appName","MyEmailApp")
loo_Json.UpdateString("service","IMAP")
loo_Json.UpdateString("domain","example2.com")
loo_Json.UpdateString("username","jane@example2.com")

ls_Password = loo_Secrets.GetSecretStr(loo_Json)
if loo_Secrets.LastMethodSuccess = 0 then
    Write-Debug loo_Secrets.LastErrorText
    destroy loo_Imap
    destroy loo_Secrets
    destroy loo_Json
    return
end if

li_Success = loo_Imap.Login("jane@example2.com",ls_Password)
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Secrets
    destroy loo_Json
    return
end if

// Select an IMAP mailbox
li_Success = loo_Imap.SelectMailbox("Inbox")
if li_Success <> 1 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Secrets
    destroy loo_Json
    return
end if

// 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.
loo_MessageSet = loo_Imap.Search("SUBJECT encrypted",1)
if loo_Imap.LastMethodSuccess = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Secrets
    destroy loo_Json
    return
end if

if loo_MessageSet.Count = 0 then
    Write-Debug "No messages found."
    destroy loo_MessageSet
    destroy loo_Imap
    destroy loo_Secrets
    destroy loo_Json
    return
end if

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

li_Uid = loo_MessageSet.GetId(0)
loo_Email = loo_Imap.FetchSingle(li_Uid,1)
if loo_Imap.LastMethodSuccess = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_MessageSet
    destroy loo_Imap
    destroy loo_Secrets
    destroy loo_Json
    return
end if

// Here we can show if the email was received encrypted, if it was successfully decrypted, and
// which certificate was used to decrypt.
Write-Debug "Email received encrypted: " + string(loo_Email.ReceivedEncrypted)
// Was it successfully decrypted?
Write-Debug "Successfully decrypted: " + string(loo_Email.Decrypted)
// What cert was used to decrypt?
Write-Debug "Encrypted by: " + loo_Email.EncryptedBy
loo_Cert = loo_Email.GetEncryptedByCert()
if loo_Email.LastMethodSuccess = 1 then
    Write-Debug "Certificate DN: " + loo_Cert.SubjectDN
    destroy loo_Cert
end if

// Show the decrypted email body.
Write-Debug loo_Email.Body

destroy loo_MessageSet
destroy loo_Email

loo_Imap.Disconnect()


destroy loo_Imap
destroy loo_Secrets
destroy loo_Json

 

© 2000-2025 Chilkat Software, Inc. All Rights Reserved.