B4X Requires Chilkat v11.0.0+
B4X
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 B4X Downloads
Dim success As Boolean = False
Dim imap As ChilkatImap
imap.Initialize("imap")
imap.Ssl = True
imap.Port = 993
success = imap.Connect("imap.example2.com")
If success = False Then
Log(imap.LastErrorText)
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
Dim secrets As ChilkatSecrets
secrets.Initialize("secrets")
' On Windows, this is the Windows Credentials Manager
' On MacOS/iOS, it is the Apple Keychain
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.
Dim json As ChilkatJsonObject
json.Initialize
json.UpdateString("appName", "MyEmailApp")
json.UpdateString("service", "IMAP")
json.UpdateString("domain", "example2.com")
json.UpdateString("username", "jane@example2.com")
Dim password As String = secrets.GetSecretStr(json)
If secrets.LastMethodSuccess = False Then
Log(secrets.LastErrorText)
Return
End If
success = imap.Login("jane@example2.com", password)
If success = False Then
Log(imap.LastErrorText)
Return
End If
' Select an IMAP mailbox
success = imap.SelectMailbox("Inbox")
If success = False Then
Log(imap.LastErrorText)
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.
Dim messageSet As ChilkatMessageSet
messageSet.Initialize
success = imap.QueryMbx("SUBJECT encrypted", True, messageSet)
If success = False Then
Log(imap.LastErrorText)
Return
End If
If messageSet.Count = 0 Then
Log("No messages found.")
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.
Dim uid As Long = messageSet.GetId(0)
Dim email As ChilkatEmail
email.Initialize
success = imap.FetchEmail(False, uid, True, email)
If success = False Then
Log(imap.LastErrorText)
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.
Log("Email received encrypted: " & email.ReceivedEncrypted)
' Was it successfully decrypted?
Log("Successfully decrypted: " & email.Decrypted)
' What cert was used to decrypt?
Log("Encrypted by: " & email.EncryptedBy)
Dim cert As ChilkatCert
cert.Initialize("cert")
success = email.LastDecryptCert(cert)
If success <> False Then
Log("Certificate DN: " & cert.SubjectDN)
End If
' Show the decrypted email body.
Log(email.Body)
imap.Disconnect