Sample code for 30+ languages & platforms
Classic ASP

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 Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

set imap = Server.CreateObject("Chilkat.Imap")

imap.Ssl = 1
imap.Port = 993
success = imap.Connect("imap.example2.com")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
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
set secrets = Server.CreateObject("Chilkat.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.
set json = Server.CreateObject("Chilkat.JsonObject")
success = json.UpdateString("appName","MyEmailApp")
success = json.UpdateString("service","IMAP")
success = json.UpdateString("domain","example2.com")
success = json.UpdateString("username","jane@example2.com")

password = secrets.GetSecretStr(json)
If (secrets.LastMethodSuccess = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( secrets.LastErrorText) & "</pre>"
    Response.End
End If

success = imap.Login("jane@example2.com",password)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

' Select an IMAP mailbox
success = imap.SelectMailbox("Inbox")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
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.

set messageSet = Server.CreateObject("Chilkat.MessageSet")
success = imap.QueryMbx("SUBJECT encrypted",1,messageSet)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

If (messageSet.Count = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "No messages found.") & "</pre>"
    Response.End
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.

uid = messageSet.GetId(0)

set email = Server.CreateObject("Chilkat.Email")
success = imap.FetchEmail(0,uid,1,email)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

' Here we can show if the email was received encrypted, if it was successfully decrypted, and
' which certificate was used to decrypt.
Response.Write "<pre>" & Server.HTMLEncode( "Email received encrypted: " & email.ReceivedEncrypted) & "</pre>"

' Was it successfully decrypted?
Response.Write "<pre>" & Server.HTMLEncode( "Successfully decrypted: " & email.Decrypted) & "</pre>"

' What cert was used to decrypt?
Response.Write "<pre>" & Server.HTMLEncode( "Encrypted by: " & email.EncryptedBy) & "</pre>"
set cert = Server.CreateObject("Chilkat.Cert")
success = email.LastDecryptCert(cert)
If (success <> 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Certificate DN: " & cert.SubjectDN) & "</pre>"
End If

' Show the decrypted email body.
Response.Write "<pre>" & Server.HTMLEncode( email.Body) & "</pre>"

success = imap.Disconnect()

%>
</body>
</html>