Visual FoxPro
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loImap
LOCAL loSecrets
LOCAL loJson
LOCAL lcPassword
LOCAL loMessageSet
LOCAL lnUid
LOCAL loEmail
LOCAL loCert
lnSuccess = 0
loImap = CreateObject('Chilkat.Imap')
loImap.Ssl = 1
loImap.Port = 993
lnSuccess = loImap.Connect("imap.example2.com")
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
CANCEL
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
loSecrets = CreateObject('Chilkat.Secrets')
* On Windows, this is the Windows Credentials Manager
* On MacOS/iOS, it is the Apple Keychain
loSecrets.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.
loJson = CreateObject('Chilkat.JsonObject')
loJson.UpdateString("appName","MyEmailApp")
loJson.UpdateString("service","IMAP")
loJson.UpdateString("domain","example2.com")
loJson.UpdateString("username","jane@example2.com")
lcPassword = loSecrets.GetSecretStr(loJson)
IF (loSecrets.LastMethodSuccess = 0) THEN
? loSecrets.LastErrorText
RELEASE loImap
RELEASE loSecrets
RELEASE loJson
CANCEL
ENDIF
lnSuccess = loImap.Login("jane@example2.com",lcPassword)
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
RELEASE loSecrets
RELEASE loJson
CANCEL
ENDIF
* Select an IMAP mailbox
lnSuccess = loImap.SelectMailbox("Inbox")
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
RELEASE loSecrets
RELEASE loJson
CANCEL
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.
loMessageSet = CreateObject('Chilkat.MessageSet')
lnSuccess = loImap.QueryMbx("SUBJECT encrypted",1,loMessageSet)
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
RELEASE loSecrets
RELEASE loJson
RELEASE loMessageSet
CANCEL
ENDIF
IF (loMessageSet.Count = 0) THEN
? "No messages found."
RELEASE loImap
RELEASE loSecrets
RELEASE loJson
RELEASE loMessageSet
CANCEL
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.
lnUid = loMessageSet.GetId(0)
loEmail = CreateObject('Chilkat.Email')
lnSuccess = loImap.FetchEmail(0,lnUid,1,loEmail)
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
RELEASE loSecrets
RELEASE loJson
RELEASE loMessageSet
RELEASE loEmail
CANCEL
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(loEmail.ReceivedEncrypted)
* Was it successfully decrypted?
? "Successfully decrypted: " + STR(loEmail.Decrypted)
* What cert was used to decrypt?
? "Encrypted by: " + loEmail.EncryptedBy
loCert = CreateObject('Chilkat.Cert')
lnSuccess = loEmail.LastDecryptCert(loCert)
IF (lnSuccess <> 0) THEN
? "Certificate DN: " + loCert.SubjectDN
ENDIF
* Show the decrypted email body.
? loEmail.Body
loImap.Disconnect()
RELEASE loImap
RELEASE loSecrets
RELEASE loJson
RELEASE loMessageSet
RELEASE loEmail
RELEASE loCert