Sample code for 30+ languages & platforms
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.

Chilkat PowerBuilder Downloads

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

li_Success = 0

loo_Imap = create oleobject
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 = 0 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
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
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 = 0 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 = 0 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 = create oleobject
li_rc = loo_MessageSet.ConnectToNewObject("Chilkat.MessageSet")

li_Success = loo_Imap.QueryMbx("SUBJECT encrypted",1,loo_MessageSet)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Secrets
    destroy loo_Json
    destroy loo_MessageSet
    return
end if

if loo_MessageSet.Count = 0 then
    Write-Debug "No messages found."
    destroy loo_Imap
    destroy loo_Secrets
    destroy loo_Json
    destroy loo_MessageSet
    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 = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Imap.FetchEmail(0,li_Uid,1,loo_Email)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Secrets
    destroy loo_Json
    destroy loo_MessageSet
    destroy loo_Email
    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 = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Email.LastDecryptCert(loo_Cert)
if li_Success <> 0 then
    Write-Debug "Certificate DN: " + loo_Cert.SubjectDN
end if

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

loo_Imap.Disconnect()


destroy loo_Imap
destroy loo_Secrets
destroy loo_Json
destroy loo_MessageSet
destroy loo_Email
destroy loo_Cert