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

Reply to a message in its existing Gmail thread

See more GMail REST API Examples

Live send. Supply the Gmail ID of a received message and use an authorized From address. Fetch its raw MIME so Chilkat.Email can parse encoded headers and recipient addresses. Send a plain-text reply to Reply-To, falling back to From. The reply goes to the original sender or reply address.

The request supplies Gmail’s threadId and matching Subject. The MIME In-Reply-To contains the original RFC Message-ID. References preserves the conversation ancestry and appends that Message-ID. Select a message with a valid Message-ID and review its recipient before running this sending example. Authorize both gmail.readonly and gmail.send, or use gmail.modify as shown below.

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

Google documentation

Chilkat Rust Downloads

Rust
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 url = chilkat::StringBuilder::new();
let _ = url.append("https://gmail.googleapis.com/gmail/v1/users/me/messages/");
let _ = url.append("GMAIL_MESSAGE_ID");
let _ = url.append("?format=raw");
success = http.http_no_body("GET", &url.get_as_string().unwrap_or_default(), &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());
    success = reply.load(&resp.body_str()).is_ok();
    if success {
        println!("{}", reply.string_of("error.message").unwrap_or_default());
    }

    return;
}

success = reply.load(&resp.body_str()).is_ok();
if !success {
    println!("Expected a Gmail JSON response.");
    return;
}

if (reply.json_type_of("threadId") != 1) || (reply.string_of_equals("threadId", "", true)) {
    println!("Expected threadId in the response.");
    return;
}

if (reply.json_type_of("raw") != 1) || (reply.string_of_equals("raw", "", true)) {
    println!("Expected raw in the response.");
    return;
}

let thread_id = reply.string_of("threadId").unwrap_or_default();
let original_mime = chilkat::BinData::new();
success = reply.bytes_of("raw", "base64url", &original_mime).is_ok();
if !success {
    println!("{}", reply.last_error_text());
    return;
}

let original = chilkat::Email::new();
success = original.set_from_mime_bd(&original_mime).is_ok();
if !success {
    println!("{}", original.last_error_text());
    return;
}

let message_id_header = original.get_header_field("Message-ID").unwrap_or_default();
if (!original.last_method_success()) || (message_id_header == "") {
    println!("The original message needs a Message-ID for this threaded reply.");
    return;
}

let email = chilkat::Email::new();
email.set_from("Sender <sender@example.com>");
// Use the parsed Reply-To mailbox list when present, otherwise use From.
let mut reply_address = original.reply_to();
if reply_address == "" {
    reply_address = original.from();
}

success = email.add_multiple_to(&reply_address).is_ok();
if !success {
    println!("{}", email.last_error_text());
    return;
}

if email.num_to() == 0 {
    println!("The original message has no usable reply address.");
    return;
}

email.set_subject(&original.subject());
email.set_charset("utf-8");
email.set_body("Thank you for your message.");
email.add_header_field("In-Reply-To", &message_id_header);
let references = chilkat::StringBuilder::new();
let previous_references = original.get_header_field("References").unwrap_or_default();
if (original.last_method_success()) && (previous_references != "") {
    let _ = references.append(&previous_references);
    let _ = references.append(" ");
}

let _ = references.append(&message_id_header);
email.add_header_field("References", &references.get_as_string().unwrap_or_default());
let mime = chilkat::BinData::new();
success = email.get_mime_bd(&mime).is_ok();
if !success {
    println!("{}", email.last_error_text());
    return;
}

let request = chilkat::JsonObject::new();
success = request.update_bd("raw", "base64url", &mime).is_ok();
if !success {
    println!("{}", request.last_error_text());
    return;
}

success = request.update_string("threadId", &thread_id).is_ok();
if !success {
    println!("{}", request.last_error_text());
    return;
}

success = http.http_json("POST", "https://gmail.googleapis.com/gmail/v1/users/me/messages/send", &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!("Sent reply ID: {}", reply.string_of("id").unwrap_or_default());
println!("Thread ID: {}", reply.string_of("threadId").unwrap_or_default());