Rust Requires Chilkat v11.0.0+
Rust
Insert an EML file into the Gmail inbox
See more GMail REST API Examples
Read an existing message.eml as bytes and insert it into the authenticated mailbox with the INBOX label in the same request. This stores a message without delivering it to recipients. Insert bypasses most scanning and classification. For mail that should undergo normal inbound processing, see messages.import.
Authorize the application with https://www.googleapis.com/auth/gmail.insert. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Rust Downloads
let mut success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let token = chilkat::JsonObject::new();
success = token.load_file("gmail-token.json").is_ok();
if !success {
println!("Could not read gmail-token.json from the private working directory.");
return;
}
if (token.json_type_of("access_token") != 1) || (token.string_of_equals("access_token", "", true)) {
println!("An OAuth access token is required.");
return;
}
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_auth_token(&token.string_of("access_token").unwrap_or_default());
let resp = chilkat::HttpResponse::new();
let reply = chilkat::JsonObject::new();
let mime = chilkat::BinData::new();
success = mime.load_file("message.eml").is_ok();
if !success {
println!("Could not read message.eml.");
return;
}
let request = chilkat::JsonObject::new();
success = request.update_bd("raw", "base64url", &mime).is_ok();
if !success {
println!("{}", request.last_error_text());
return;
}
let _ = request.update_string("labelIds[0]", "INBOX");
success = http.http_json("POST", "https://gmail.googleapis.com/gmail/v1/users/me/messages", &request, "application/json", &resp).is_ok();
if resp.status_code() == 0 {
println!("Gmail did not return an HTTP response.");
return;
}
if (!success) || ((resp.status_code() < 200) || (resp.status_code() >= 300)) {
println!("Gmail HTTP {}", resp.status_code());
if reply.load(&resp.body_str()).is_ok() {
println!("{}", reply.string_of("error.message").unwrap_or_default());
}
return;
}
if reply.load(&resp.body_str()).is_err() {
println!("Expected a Gmail JSON response.");
return;
}
if (reply.json_type_of("id") != 1) || (reply.string_of_equals("id", "", true)) {
println!("Expected id in the response.");
return;
}
println!("Gmail message ID: {}", reply.string_of("id").unwrap_or_default());