Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
let imap = CkoImap()!
imap.ssl = true
imap.port = 993
success = imap.connect(hostname: "imap.example2.com")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// 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
let secrets = CkoSecrets()!
// 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.
let json = CkoJsonObject()!
json.updateString(jsonPath: "appName", value: "MyEmailApp")
json.updateString(jsonPath: "service", value: "IMAP")
json.updateString(jsonPath: "domain", value: "example2.com")
json.updateString(jsonPath: "username", value: "jane@example2.com")
var password: String? = secrets.getSecretStr(jsonId: json)
if secrets.lastMethodSuccess == false {
print("\(secrets.lastErrorText!)")
return
}
success = imap.login(login: "jane@example2.com", password: password)
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Select an IMAP mailbox
success = imap.selectMailbox(mailbox: "Inbox")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// 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.
let messageSet = CkoMessageSet()!
success = imap.queryMbx(criteria: "SUBJECT encrypted", bUid: true, msgSet: messageSet)
if success == false {
print("\(imap.lastErrorText!)")
return
}
if messageSet.count.intValue == 0 {
print("No messages found.")
return
}
// 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.
var uid: UInt32 = messageSet.getId(index: 0)
let email = CkoEmail()!
success = imap.fetchEmail(headerOnly: false, msgId: uid, bUid: true, email: email)
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Here we can show if the email was received encrypted, if it was successfully decrypted, and
// which certificate was used to decrypt.
print("Email received encrypted: \(email.receivedEncrypted)")
// Was it successfully decrypted?
print("Successfully decrypted: \(email.decrypted)")
// What cert was used to decrypt?
print("Encrypted by: \(email.encryptedBy!)")
let cert = CkoCert()!
success = email.lastDecryptCert(cert: cert)
if success != false {
print("Certificate DN: \(cert.subjectDN!)")
}
// Show the decrypted email body.
print("\(email.body!)")
imap.disconnect()
}