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

Find and save an attachment and list inline images

See more Email Object Examples

Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let email = CkoEmail()!
    success = email.loadEml(mimePath: "with-attachments.eml")
    if success != true {
        print("\(email.lastErrorText!)")
        return
    }

    let filename = CkoStringBuilder()!
    let mediaType = CkoStringBuilder()!
    var found: Bool = false
    var i: Int = 0
    while (i < email.numAttachments.intValue) {
        filename.setString(value: email.getAttachmentFilename(index: i))
        mediaType.setString(value: email.getAttachmentContentType(index: i))
        if (filename.contentsEqual(str: "agenda.csv", caseSensitive: false) == true) && (mediaType.contentsEqual(str: "text/csv", caseSensitive: false) == true) && (found == false) {
            let data = CkoBinData()!
            success = email.getAttachmentBd(index: i, binData: data)
            if success != true {
                print("\(email.lastErrorText!)")
                return
            }

            success = data.writeFile(path: "received-agenda.csv")
            if success != true {
                print("Failed: data.WriteFile.")
                return
            }

            print("Saved attachment: \(data.numBytes.intValue) bytes.")
            found = true
        }

        i = i + 1
    }

    if found != true {
        print("No matching attachment.")
    }

    i = 0
    while (i < email.numRelatedItems.intValue) {
        print("Related: \(email.getRelatedContentType(index: i)!)  Content-ID: \(email.getRelatedContentID(index: i)!)")
        i = i + 1
    }


}