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

Extract signed invoice XML from an Italian PEC message

See more Email Object Examples

Search the PEC folder by subject, fetch one envelope by UID, and inspect the attachments of each nested email. Match .xml.p7m by filename and verify the opaque CMS signature before loading the XML. This checks signature integrity. Certificate trust and invoice schema validation are separate application decisions.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    let imap = CkoImap()!
    imap.ssl = true
    imap.port = 993
    imap.requireSslCertVerify = true
    imap.peekMode = true
    imap.autoDownloadAttachments = true
    success = imap.connect(hostname: "imap.example.com")
    if success != true {
        print("\(imap.lastErrorText!)")
        return
    }

    success = imap.login(login: "IMAP_USERNAME", password: "IMAP_PASSWORD")
    if success != true {
        print("\(imap.lastErrorText!)")
        return
    }

    success = imap.selectMailbox(mailbox: "INBOX/postacert")
    if success != true {
        print("\(imap.lastErrorText!)")
        return
    }

    let messages = CkoMessageSet()!
    success = imap.queryMbx(criteria: "SUBJECT \"POSTA CERTIFICATA: Invio File\"", bUid: true, msgSet: messages)
    if success != true {
        print("\(imap.lastErrorText!)")
        return
    }

    if messages.count.intValue == 0 {
        print("No matching PEC envelope.")
        imap.disconnect()
        return
    }

    let envelope = CkoEmail()!
    success = imap.fetchEmail(headerOnly: false, msgId: messages.getId(index: 0), bUid: true, email: envelope)
    if success != true {
        print("\(imap.lastErrorText!)")
        return
    }

    imap.disconnect()
    let nested = CkoEmail()!
    let name = CkoStringBuilder()!
    var verified: Int = 0
    var i: Int = 0
    while (i < envelope.numAttachedMessages.intValue) {
        success = envelope.getAttached(index: i, email: nested)
        if success != true {
            print("\(envelope.lastErrorText!)")
            return
        }

        var j: Int = 0
        while (j < nested.numAttachments.intValue) {
            name.setString(value: nested.getAttachmentFilename(index: j))
            if name.ends(with: ".xml.p7m", caseSensitive: false) == true {
                let data = CkoBinData()!
                success = nested.getAttachmentBd(index: j, binData: data)
                if success != true {
                    print("\(nested.lastErrorText!)")
                    return
                }

                let cms = CkoCrypt2()!
                success = cms.opaqueVerifyBd(bd: data)
                if success != true {
                    print("\(cms.lastErrorText!)")
                    return
                }

                let invoice = CkoXml()!
                success = invoice.loadBd(bd: data, autoTrim: true)
                if success != true {
                    print("\(invoice.lastErrorText!)")
                    return
                }

                print("\(name.getAsString()!)  XML root: \(invoice.tag!)")
                verified = verified + 1
            }

            j = j + 1
        }

        i = i + 1
    }

    print("Verified XML invoices: \(verified)")

}