Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.0.0+

IMAP Download and Verify Signed (S/MIME) Email

See more IMAP Examples

Demonstrates how to download and verify digitally signed S/MIME email.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    let imap = CkoImap()!

    //  Connect to an IMAP server.
    //  Use TLS
    imap.ssl = true
    imap.port = 993
    success = imap.connect(hostname: "imap.example.com")
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    success = imap.login(login: "myLogin", password: "myPassword")
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    //  Select an IMAP mailbox
    success = imap.selectMailbox(mailbox: "Inbox")
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    //  We can choose to fetch UIDs or sequence numbers.
    var fetchUids: Bool = true

    //  Get the message IDs of all the emails in the mailbox
    let messageSet = CkoMessageSet()!
    success = imap.queryMbx(criteria: "ALL", bUid: fetchUids, msgSet: messageSet)
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    let email = CkoEmail()!
    let cert = CkoCert()!

    var i: Int = 0
    while i < messageSet.count.intValue {

        var uid: String? = messageSet.getId(index: i)
        print("uid: \(uid!)")

        success = imap.fetchEmail(headerOnly: false, msgId: uid, bUid: true, email: email)
        if success == false {
            print("\(imap.lastErrorText!)")
            return
        }

        //  The security layers of signed and/or encrypted emails
        //  are automatically "unwrapped" when loaded into
        //  a Chilkat email object.
        //  An application only needs to check to see if an email
        //  was received signed or encrypted, and then examine
        //  the success/failure.  For example:
        if email.receivedSigned == true {

            print("This email was signed.")

            //  Check to see if the signatures were verified.
            if email.signaturesValid == true {
                print("Digital signature(s) verified.")
                print("Signer: \(email.signedBy!)")

                //  Get the certificate used for signing.
                success = email.lastSignerCert(index: 0, cert: cert)

                if success == false {
                    print("Failed to get signing certificate object.")
                }
                else {
                    print("Signing cert: \(cert.subjectCN!)")
                }

            }
            else {
                print("Digital signature verification failed.")
            }

        }

        i = i + 1
    }

    //  Disconnect from the IMAP server.
    success = imap.disconnect()

}