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

Insert Email into GMail Folder

See more GMail REST API Examples

Directly inserts a message into only this user's mailbox similar to IMAP APPEND, bypassing most scanning and classification. Does not send a message.

This example will create a Chilkat Email object and upload/insert it into GMail. Then adds the "INBOX" label to the email to make it appear in INBOX

Chilkat Rust Downloads

Rust

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

let email = chilkat::Email::new();

// This example chooses to load an email from a file.
if email.load_eml("qa_data/eml/testEmail.eml").is_err() {
    println!("{}", email.last_error_text());
    return;
}

// Get the email as MIME.
let sb_mime = chilkat::StringBuilder::new();
let _ = email.get_mime_sb(&sb_mime);

let http = chilkat::Http::new();
let resp = chilkat::HttpResponse::new();

http.set_auth_token("GMAIL-ACCESS-TOKEN");

// Upload to GMail (does not send, but just inserts).

// IMPORTANT: After uploading/inserting, you won't see the message in the Inbox.
// In your web browser, if you go to "All Mail", you will see it.

let mut url = "https://www.googleapis.com/upload/gmail/v1/users/me/messages?uploadType=media".to_string();
http.set_uncommon_options("SendGzipped");
if http.http_sb("POST", &url, &sb_mime, "utf-8", "message/rfc822", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

http.set_uncommon_options("");

// A status code of 200 indicates success.
println!("Response status code: {}", resp.status_code());
println!("Response body:");
println!("{}", resp.body_str());

// The response body contains JSON.
// A sample successful JSON response:

// {
//  "id": "166f583051d36144",
//  "threadId": "166f5529e079a456"
// }

let json = chilkat::JsonObject::new();
let _ = json.load(&resp.body_str());
let id = json.string_of("id").unwrap_or_default();

// Add the Inbox label to the inserted email so that it appears in Inbox.
println!("---- Adding the Inbox label to the newly inserted email. ----");

// Create the following JSON to be sent in a POST to modify labels for this message.
// {
//   "addLabelIds": [
//     "INBOX"
//     ]
// }

let json2 = chilkat::JsonObject::new();
let _ = json2.update_string("addLabelIds[0]", "INBOX");

let _ = http.set_url_var("id", &id);
url = "https://www.googleapis.com/gmail/v1/users/me/messages/{$id}/modify".to_string();

if http.http_json("POST", &url, &json2, "application/json", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

// A status code of 200 indicates success.
println!("Response status code: {}", resp.status_code());
println!("Response body:");
println!("{}", resp.body_str());

// The response body contains JSON.
// A sample successful JSON response:

// {
//  "id": "166f583051d36144",
//  "threadId": "166f583051d36144",
//  "labelIds": [
//   "INBOX"
//  ]
// }