Sample code for 30+ languages & platforms
Rust

Outlook Send Email using MIME Format

See more Outlook Examples

This example sends an email using MIME format via the Microsoft Graph Outlook REST API.

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();

// Use your previously obtained access token here:
// See the following examples for getting an access token:
//    Get Microsoft Graph OAuth2 Access Token (Azure AD v2.0 Endpoint).
//    Get Microsoft Graph OAuth2 Access Token (Azure AD Endpoint).
//    Refresh Access Token (Azure AD v2.0 Endpoint).
//    Refresh Access Token (Azure AD Endpoint).

let json_token = chilkat::JsonObject::new();
if json_token.load_file("qa_data/tokens/microsoftGraph.json").is_err() {
    println!("{}", json_token.last_error_text());
    return;
}

http.set_auth_token(&json_token.string_of("access_token").unwrap_or_default());

// To send email, we'll POST to the following endpoint:
// 
// POST /users/{id | userPrincipalName}/sendMail
// 
// (The special keyword "me" may be used in place of a principal name.)
// 

// The body of the POST request will contain the MIME source of the email in base64 format.

// Create a new email object
let email = chilkat::Email::new();
email.set_subject("Test Outlook API to Send HTML Email with Attachments");
email.set_from("Mary <mary@somewhere.com>");
let _ = email.add_to("Joe", "joe@example.com");

// Add a plain-text alternative body, which will likely never be seen.
// (It is shown if the receiving email client is incapable of displaying HTML email.)
let _ = email.add_plain_text_alternative_body("This is a plain-text alternative body...");

// Our HTML will include an image, so add the related image here.
let Ok(content_id_starfish) = email.add_related_file("qa_data/jpg/starfish.jpg") else {
    println!("{}", email.last_error_text());
    return;
};

// The src attribute for the image tag is set to the contentIdStarfish:
let sb_html = chilkat::StringBuilder::new();
let _ = sb_html.append("<html><body><p>This is an HTML email with an embedded image.</p>");
let _ = sb_html.append("<p><img src=\"cid:CONTENT_ID_STARFISH\" /></p></body></html>");
let num_replacements = sb_html.replace("CONTENT_ID_STARFISH", &content_id_starfish);

let _ = email.add_html_alternative_body(&sb_html.get_as_string().unwrap_or_default());

// Finally, add some attachments to the email.
// Add a file attachment.
if email.add_file_attachment2("qa_data/pdf/fishing.pdf", "application/pdf").is_err() {
    println!("{}", email.last_error_text());
    return;
}

// Add an attachment where the content is contained in a string.
let content = "This is the content of the 2nd attached file.".to_string();
let _ = email.add_string_attachment("someText.txt", &content);

// Get the email as multi-line base64..
let bd_mime = chilkat::BinData::new();
let _ = email.get_mime_bd(&bd_mime);

// Now get it as multi-line base64
let sb_base64 = chilkat::StringBuilder::new();
let _ = bd_mime.get_encoded_sb("base64_mime", &sb_base64);

// Send the HTTP POST (i.e. send the email)
let resp = chilkat::HttpResponse::new();
if http.http_sb("POST", "https://graph.microsoft.com/v1.0/me/sendMail", &sb_base64, "utf-8", "text/plain", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

// The send succeeded if the response status code = 202.
// In the success case, there is no response body.  (We just get the response code to know that it succeeded.)
if resp.status_code() != 202 {
    let json = chilkat::JsonObject::new();
    let _ = json.load(&resp.body_str());
    json.set_emit_compact(false);
    println!("{}", json.emit().unwrap_or_default());
    println!("Failed, response status code = {}", resp.status_code());
} else {
    println!("Outlook Mail Sent.");
}