Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oSecrets
LOCAL oJson
LOCAL cPassword
LOCAL oMessageSet
LOCAL nUid
LOCAL oEmail
LOCAL oCert

nSuccess := 0

oImap := CreateObject("Chilkat.Imap")

oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example2.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
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
oSecrets := CreateObject("Chilkat.Secrets")

//  On Windows, this is the Windows Credentials Manager
//  On MacOS/iOS, it is the Apple Keychain
oSecrets: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.
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("appName", "MyEmailApp")
oJson:UpdateString("service", "IMAP")
oJson:UpdateString("domain", "example2.com")
oJson:UpdateString("username", "jane@example2.com")

cPassword := oSecrets:GetSecretStr(oJson)
IF (oSecrets:LastMethodSuccess == 0)
    ? oSecrets:LastErrorText
    oImap:destroy()
    oSecrets:destroy()
    oJson:destroy()
    RETURN
ENDIF

nSuccess := oImap:Login("jane@example2.com", cPassword)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oSecrets:destroy()
    oJson:destroy()
    RETURN
ENDIF

//  Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oSecrets:destroy()
    oJson:destroy()
    RETURN
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.

oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("SUBJECT encrypted", 1, oMessageSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oSecrets:destroy()
    oJson:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

IF (oMessageSet:Count == 0)
    ? "No messages found."
    oImap:destroy()
    oSecrets:destroy()
    oJson:destroy()
    oMessageSet:destroy()
    RETURN
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.

nUid := oMessageSet:GetId(0)

oEmail := CreateObject("Chilkat.Email")
nSuccess := oImap:FetchEmail(0, nUid, 1, oEmail)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oSecrets:destroy()
    oJson:destroy()
    oMessageSet:destroy()
    oEmail:destroy()
    RETURN
ENDIF

//  Here we can show if the email was received encrypted, if it was successfully decrypted, and
//  which certificate was used to decrypt.
? "Email received encrypted: " + Str(oEmail:ReceivedEncrypted)

//  Was it successfully decrypted?
? "Successfully decrypted: " + Str(oEmail:Decrypted)

//  What cert was used to decrypt?
? "Encrypted by: " + oEmail:EncryptedBy
oCert := CreateObject("Chilkat.Cert")
nSuccess := oEmail:LastDecryptCert(oCert)
IF (nSuccess != 0)
    ? "Certificate DN: " + oCert:SubjectDN
ENDIF

//  Show the decrypted email body.
? oEmail:Body

oImap:Disconnect()

oImap:destroy()
oSecrets:destroy()
oJson:destroy()
oMessageSet:destroy()
oEmail:destroy()
oCert:destroy()