Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkJsonObject.pb"
IncludeFile "CkMessageSet.pb"
IncludeFile "CkImap.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkSecrets.pb"
IncludeFile "CkCert.pb"

Procedure ChilkatExample()

    success.i = 0

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    success = CkImap::ckConnect(imap,"imap.example2.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    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
    secrets.i = CkSecrets::ckCreate()
    If secrets.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; On Windows, this is the Windows Credentials Manager
    ; On MacOS/iOS, it is the Apple Keychain
    CkSecrets::setCkLocation(secrets, "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.
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"appName","MyEmailApp")
    CkJsonObject::ckUpdateString(json,"service","IMAP")
    CkJsonObject::ckUpdateString(json,"domain","example2.com")
    CkJsonObject::ckUpdateString(json,"username","jane@example2.com")

    password.s = CkSecrets::ckGetSecretStr(secrets,json)
    If CkSecrets::ckLastMethodSuccess(secrets) = 0
        Debug CkSecrets::ckLastErrorText(secrets)
        CkImap::ckDispose(imap)
        CkSecrets::ckDispose(secrets)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"jane@example2.com",password)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkSecrets::ckDispose(secrets)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ; Select an IMAP mailbox
    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkSecrets::ckDispose(secrets)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    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.

    messageSet.i = CkMessageSet::ckCreate()
    If messageSet.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckQueryMbx(imap,"SUBJECT encrypted",1,messageSet)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkSecrets::ckDispose(secrets)
        CkJsonObject::ckDispose(json)
        CkMessageSet::ckDispose(messageSet)
        ProcedureReturn
    EndIf

    If CkMessageSet::ckCount(messageSet) = 0
        Debug "No messages found."
        CkImap::ckDispose(imap)
        CkSecrets::ckDispose(secrets)
        CkJsonObject::ckDispose(json)
        CkMessageSet::ckDispose(messageSet)
        ProcedureReturn
    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.

    uid.i = CkMessageSet::ckGetId(messageSet,0)

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckFetchEmail(imap,0,uid,1,email)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkSecrets::ckDispose(secrets)
        CkJsonObject::ckDispose(json)
        CkMessageSet::ckDispose(messageSet)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    ; Here we can show if the email was received encrypted, if it was successfully decrypted, and
    ; which certificate was used to decrypt.
    Debug "Email received encrypted: " + Str(CkEmail::ckReceivedEncrypted(email))

    ; Was it successfully decrypted?
    Debug "Successfully decrypted: " + Str(CkEmail::ckDecrypted(email))

    ; What cert was used to decrypt?
    Debug "Encrypted by: " + CkEmail::ckEncryptedBy(email)
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkEmail::ckLastDecryptCert(email,cert)
    If success <> 0
        Debug "Certificate DN: " + CkCert::ckSubjectDN(cert)
    EndIf

    ; Show the decrypted email body.
    Debug CkEmail::ckBody(email)

    CkImap::ckDisconnect(imap)


    CkImap::ckDispose(imap)
    CkSecrets::ckDispose(secrets)
    CkJsonObject::ckDispose(json)
    CkMessageSet::ckDispose(messageSet)
    CkEmail::ckDispose(email)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure