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

Download the original Gmail message as EML

See more GMail REST API Examples

Supply a Gmail message ID and request format=raw. Decode the complete MIME message directly to bytes and save them in downloaded.eml. Parse those same bytes with Chilkat.Email to read the subject, sender, and attachment count without a text-encoding round trip.

Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.

Google documentation

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let token = CkoJsonObject()!
    success = token.loadFile(path: "gmail-token.json")
    if success != true {
        print("Could not read gmail-token.json from the private working directory.")
        return
    }

    if (token.jsonType(of: "access_token").intValue != 1) || (token.string(ofEquals: "access_token", value: "", caseSensitive: true) == true) {
        print("An OAuth access token is required.")
        return
    }

    let http = CkoHttp()!
    http.requireSslCertVerify = true
    http.authToken = token.string(of: "access_token")
    let resp = CkoHttpResponse()!
    let reply = CkoJsonObject()!
    let url = CkoStringBuilder()!
    url.append(value: "https://gmail.googleapis.com/gmail/v1/users/me/messages/")
    url.append(value: "GMAIL_MESSAGE_ID")
    url.append(value: "?format=raw")
    success = http.httpNoBody(verb: "GET", url: url.getAsString(), response: resp)
    if resp.statusCode.intValue == 0 {
        print("Gmail did not return an HTTP response.")
        return
    }

    if (success != true) || (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
        print("Gmail HTTP \(resp.statusCode.intValue)")
        success = reply.load(json: resp.bodyStr)
        if success == true {
            print("\(reply.string(of: "error.message")!)")
        }

        return
    }

    success = reply.load(json: resp.bodyStr)
    if success != true {
        print("Expected a Gmail JSON response.")
        return
    }

    if (reply.jsonType(of: "raw").intValue != 1) || (reply.string(ofEquals: "raw", value: "", caseSensitive: true) == true) {
        print("Expected raw in the response.")
        return
    }

    let mime = CkoBinData()!
    success = reply.bytes(of: "raw", encoding: "base64url", bd: mime)
    if success != true {
        print("Could not decode the raw MIME.")
        return
    }

    success = mime.writeFile(path: "downloaded.eml")
    if success != true {
        print("Could not write downloaded.eml.")
        return
    }

    let email = CkoEmail()!
    success = email.set(fromMimeBd: mime)
    if success != true {
        print("\(email.lastErrorText!)")
        return
    }

    print("From: \(email.from!)")
    print("Subject: \(email.subject!)")
    print("Attachments: \(email.numAttachments.intValue)")

}