Sample code for 30+ languages & platforms
React Native

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 React Native Downloads

React Native
import { BinData, Email, Http, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const http = new Http();

  // 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).

  const jsonToken = new JsonObject();
  try {
    jsonToken.loadFile('qa_data/tokens/microsoftGraph.json');
  } catch {
    console.log(jsonToken.lastErrorText);
    return;
  }

  http.authToken = jsonToken.stringOf('access_token');

  // 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
  const email = new Email();
  email.subject = 'Test Outlook API to Send HTML Email with Attachments';
  email.from = 'Mary <mary@somewhere.com>';
  email.addTo('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.)
  email.addPlainTextAlternativeBody('This is a plain-text alternative body...');

  // Our HTML will include an image, so add the related image here.
  let contentIdStarfish: string;
  try {
    contentIdStarfish = email.addRelatedFile('qa_data/jpg/starfish.jpg');
  } catch {
    console.log(email.lastErrorText);
    return;
  }

  // The src attribute for the image tag is set to the contentIdStarfish:
  const sbHtml = new StringBuilder();
  sbHtml.append('<html><body><p>This is an HTML email with an embedded image.</p>');
  sbHtml.append('<p><img src="cid:CONTENT_ID_STARFISH" /></p></body></html>');
  const numReplacements = sbHtml.replace('CONTENT_ID_STARFISH', contentIdStarfish);

  email.addHtmlAlternativeBody(sbHtml.getAsString());

  // Finally, add some attachments to the email.
  // Add a file attachment.
  try {
    email.addFileAttachment2('qa_data/pdf/fishing.pdf', 'application/pdf');
  } catch {
    console.log(email.lastErrorText);
    return;
  }

  // Add an attachment where the content is contained in a string.
  const content = 'This is the content of the 2nd attached file.';
  email.addStringAttachment('someText.txt', content);

  // Get the email as multi-line base64..
  const bdMime = new BinData();
  email.getMimeBd(bdMime);

  // Now get it as multi-line base64
  const sbBase64 = new StringBuilder();
  bdMime.getEncodedSb('base64_mime', sbBase64);

  // Send the HTTP POST (i.e. send the email)
  const resp = new HttpResponse();
  try {
    await http.httpSbAsync('POST', 'https://graph.microsoft.com/v1.0/me/sendMail', sbBase64, 'utf-8', 'text/plain', resp);
  } catch {
    console.log(http.lastErrorText);
    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.statusCode !== 202) {
    const json = new JsonObject();
    json.load(resp.bodyStr);
    json.emitCompact = false;
    console.log(json.emit());
    console.log(`Failed, response status code = ${resp.statusCode}`);
  } else {
    console.log('Outlook Mail Sent.');
  }
}