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

Read text and binary MIME parts by content type

See more Email Object Examples

Use the same wildcard and inclusion flags for both counting and retrieval. Request decoded text for textual parts and BinData for binary parts. Use GetAttachedEmail when the goal is to inspect a nested email as a message.

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: "workshop.eml")
    if success != true {
        print("\(email.lastErrorText!)")
        return
    }

    var types: String? = "*/*"
    var inlineOnly: Bool = false
    var excludeAttachments: Bool = false
    var count: Int = email.getNumParts(ofType: types, inlineOnly: inlineOnly, excludeAttachments: excludeAttachments).intValue
    let mediaType = CkoStringBuilder()!
    var i: Int = 0
    while (i < count) {
        mediaType.setString(value: email.getNthContentType(index: i, contentType: types, inlineOnly: inlineOnly, excludeAttachments: excludeAttachments))
        if mediaType.starts(with: "text/", caseSensitive: false) == true {
            var partText: String? = email.getNthTextPart(ofType: i, contentType: types, inlineOnly: inlineOnly, excludeAttachments: excludeAttachments)
            if email.lastMethodSuccess != true {
                print("\(email.lastErrorText!)")
                return
            }

            print("\(mediaType.getAsString()!)  \(partText!)")
        }
        else {
            let data = CkoBinData()!
            success = email.getNthBinaryPart(ofTypeBd: i, contentType: types, inlineOnly: inlineOnly, excludeAttachments: excludeAttachments, bd: data)
            if success != true {
                print("\(email.lastErrorText!)")
                return
            }

            print("\(mediaType.getAsString()!)  \(data.numBytes.intValue) bytes")
        }

        i = i + 1
    }


}