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

Send a Gmail message with a file attachment

See more GMail REST API Examples

Live send. Prepare a small file named attachment.pdf and replace the sample addresses. Add the file as a MIME attachment and upload the complete message as message/rfc822. The response is a Gmail Message. For larger uploads or unreliable connections, see Google’s resumable upload guidance.

Authorize the application with https://www.googleapis.com/auth/gmail.send. 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 email = chilkat::Email::new();
// Use the authorized mailbox or one of its configured send-as addresses.
email.set_from("Sender <sender@example.com>");
success = email.add_to("Recipient", "recipient@example.net").is_ok();
if !success {
    println!("{}", email.last_error_text());
    return;
}

email.set_subject("Sample message");
email.set_charset("utf-8");
email.set_body("Hello from the Gmail API.");
let file_data = chilkat::BinData::new();
success = file_data.load_file("attachment.pdf").is_ok();
if !success {
    println!("Could not read attachment.pdf.");
    return;
}

success = email.add_attachment_bd("attachment.pdf", &file_data, "application/pdf").is_ok();
if !success {
    println!("{}", email.last_error_text());
    return;
}

let mime = chilkat::BinData::new();
success = email.get_mime_bd(&mime).is_ok();
if !success {
    println!("{}", email.last_error_text());
    return;
}

success = http.http_bd("POST", "https://gmail.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media", &mime, "message/rfc822", &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());