Sample code for 30+ languages & platforms
Rust

Download a Specific GMail Message into a Chilkat Email Object

See more GMail REST API Examples

Demonstrates how to download a GMail message into a Chilkat Email object.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let http = chilkat::Http::new();
http.set_auth_token("GMAIL-ACCESS-TOKEN");

// The id of the GMail message to download.
let id = "166e50fed0b9b0cb".to_string();
let user_id = "me".to_string();

let _ = http.set_url_var("userId", "me");
let _ = http.set_url_var("id", &id);

// Fetch the email.
let url = "https://www.googleapis.com/gmail/v1/users/{$userId}/messages/{$id}?format=raw".to_string();
let sb_json = chilkat::StringBuilder::new();
if http.download_sb(&url, "utf-8", &sb_json).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let json = chilkat::JsonObject::new();
let _ = json.load_sb(&sb_json);
json.set_emit_compact(false);

if http.last_status() != 200 {
    println!("{}", json.emit().unwrap_or_default());
    println!("Failed.");
    return;
}

// The returned JSON contains something like this:

// {
//   "id": "166e50fed0b9b0cb",
//   "threadId": "166e50fed0b9b0cb",
//   "labelIds": [
//     "CATEGORY_SOCIAL",
//     "INBOX"
//   ],
//   "snippet": "...",
//   "historyId": "582477",
//   "internalDate": "1541441317000",
//   "sizeEstimate": 28603,
//   "raw": "BASE64URL_CONTENT"
// }

// The RFC822 MIME of the email is contained in the "raw" as a base64URL encoded string.
// Let's decode and load into a Chilkat email object..
let sb_raw = chilkat::StringBuilder::new();
let _ = json.string_of_sb("raw", &sb_raw);
let _ = sb_raw.decode("base64url", "utf-8");

let email = chilkat::Email::new();
let _ = email.set_from_mime_sb(&sb_raw);

// Now we can use the email API to do whatever we desire..
println!("From: {}", email.from_address());
println!("Subject: {}", email.subject());
// ...