Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

$oImap = ObjCreate("Chilkat.Imap")

$oImap.Ssl = True
$oImap.Port = 993
$bSuccess = $oImap.Connect("imap.example2.com")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
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 = ObjCreate("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 = ObjCreate("Chilkat.JsonObject")
$oJson.UpdateString("appName","MyEmailApp")
$oJson.UpdateString("service","IMAP")
$oJson.UpdateString("domain","example2.com")
$oJson.UpdateString("username","jane@example2.com")

Local $sPassword = $oSecrets.GetSecretStr($oJson)
If ($oSecrets.LastMethodSuccess = False) Then
    ConsoleWrite($oSecrets.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oImap.Login("jane@example2.com",$sPassword)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

; Select an IMAP mailbox
$bSuccess = $oImap.SelectMailbox("Inbox")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
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 = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx("SUBJECT encrypted",True,$oMessageSet)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

If ($oMessageSet.Count = 0) Then
    ConsoleWrite("No messages found." & @CRLF)
    Exit
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.

Local $iUid = $oMessageSet.GetId(0)

$oEmail = ObjCreate("Chilkat.Email")
$bSuccess = $oImap.FetchEmail(False,$iUid,True,$oEmail)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

; Here we can show if the email was received encrypted, if it was successfully decrypted, and
; which certificate was used to decrypt.
ConsoleWrite("Email received encrypted: " & $oEmail.ReceivedEncrypted & @CRLF)

; Was it successfully decrypted?
ConsoleWrite("Successfully decrypted: " & $oEmail.Decrypted & @CRLF)

; What cert was used to decrypt?
ConsoleWrite("Encrypted by: " & $oEmail.EncryptedBy & @CRLF)
$oCert = ObjCreate("Chilkat.Cert")
$bSuccess = $oEmail.LastDecryptCert($oCert)
If ($bSuccess <> False) Then
    ConsoleWrite("Certificate DN: " & $oCert.SubjectDN & @CRLF)
EndIf

; Show the decrypted email body.
ConsoleWrite($oEmail.Body & @CRLF)

$oImap.Disconnect()