Sample code for 30+ languages & platforms
Swift

IMAP Download and Verify Signed MIME

See more IMAP Examples

Downloads the original MIME of a digitally signed email and saves the .p7s signature along with other MIME parts. It then imports the email into a Chilkat email object to unwrap the S/MIME and verify the signature, and subsequently saves the attachments if they haven't been saved already.

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
    }

    // Download the 1st email (as MIME) in the Inbox by sequence number.
    let sbMime = CkoStringBuilder()!
    success = imap.fetchSingle(asMimeSb: 1, bUid: false, sbMime: sbMime)
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    // Load it into a MIME object and check to see if it is signed
    let mime = CkoMime()!
    mime.loadSb(sb: sbMime)
    var alreadySavedParts: Bool = false
    if mime.containsSignedParts() == true {

        // This will save the .p7s and other parts...
        let st = CkoStringTable()!
        success = mime.parts(toFiles: "c:/temp/qa_output", st: st)
        if success == true {
            var numFiles: Int = st.count.intValue
            var i: Int = 0
            while i < numFiles {
                print("Created: \(st.string(at: i)!)")
                i = i + 1
            }

            alreadySavedParts = true
        }

    }

    // Load the MIME into an Email object.  This unwraps the security layers and verifies signatures.
    let email = CkoEmail()!
    email.set(fromMimeSb: sbMime)

    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!)")

            // The certificate used for signing may be obtained
            // by calling email.GetSignedByCert.
            let cert = CkoCert()!
            success = email.lastSignerCert(index: i, cert: cert)
            if success == false {
                print("Failed to get signing certificate object.")
            }
            else {
                print("Signing cert: \(cert.subjectCN!)")
            }

        }

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

    if alreadySavedParts != true {
        email.saveAllAttachments(directory: "c:/temp/qa_output")
    }


}