Edit

Share via


Get or set the body of a message or appointment in Outlook

Call the Body API on a message or appointment to retrieve content, determine its format, or update content. With the available Body methods, you can customize signatures depending on mail item recipients or add disclaimers for legal purposes.

Select the applicable tab to learn how to get or set the body of a mail item.

You can get the body of a message or appointment in both read and compose modes. To retrieve the body of a mail item, call Office.context.mailbox.item.body.getAsync. When you call the getAsync method, you must specify the format for the returned body in the coercionType parameter. For example, you can get the body in HTML or plain text format.

The following example gets the body of an item in HTML format.

// Get the current body of the message or appointment.
Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html, (bodyResult) => {
  if (bodyResult.status === Office.AsyncResultStatus.Failed) {
    console.log(`Failed to get body: ${bodyResult.error.message}`);
    return;
  }

  const body = bodyResult.value;

  // Perform additional operations here.
});

Get the body of message replies in Outlook on the web, on mobile, or in the new Outlook on Windows

In Outlook on the web, on mobile devices, and in the new Outlook on Windows, when you call Office.context.mailbox.item.body.getAsync on a message reply, the entire body of a conversation thread is returned. If you only need the current reply, you can specify the bodyMode option in the getAsync call.

In Outlook on the web and the new Outlook on Windows, you can use the bodyMode option to reflect a user's Message Organization setting. Users can organize their messages as conversations or individual messages in Settings > Mail > Layout > Message organization. This setting affects how much of a message's body is displayed to the user, particularly in conversation threads with multiple messages. Depending on the setting, the contents of the entire conversation thread or just the current message is displayed. For more information on the Message Organization setting, see Change how the message list is displayed in Outlook.

The following table lists the portion of the body returned depending on the bodyMode configuration.

bodyMode configuration Effect on body returned
bodyMode isn't specified in the getAsync call The entire body of the conversation thread is returned. However, in Outlook on mobile, while in quick reply mode (the reply field at the bottom of the message), only the body of the current reply is returned.
bodyMode is set to Office.MailboxEnums.BodyMode.FullBody The entire body of the conversation thread is returned. However, in Outlook on mobile, while in quick reply mode, only the body of the current reply is returned.
bodyMode is set to Office.MailboxEnums.BodyMode.HostConfig In Outlook on the web and the new Outlook on Windows, if Message Organization is set to Group messages by conversation > All messages from the selected conversation or Show email grouped by conversation > Newest on top/Newest on bottom, only the body of the current reply is returned. Conversely, if Message Organization is set to Individual messages: Do not group messages > Only a single message or Show email as individual messages, the entire body of the conversation thread is returned.

In Outlook on mobile, only the body of the current reply is returned.

Note

  • In Outlook on mobile devices, the bodyMode option is available starting with Version 4.2538.0.
  • The bodyMode option is ignored in Outlook on Windows (classic) and on Mac.

The following example specifies the bodyMode option to honor the user's message setting.

Office.context.mailbox.item.body.getAsync(
  Office.CoercionType.Html,
  { bodyMode: Office.MailboxEnums.BodyMode.HostConfig },
  (bodyResult) => {
    if (bodyResult.status === Office.AsyncResultStatus.Failed) {
      console.log(`Failed to get body: ${bodyResult.error.message}`);
      return;
    }

    const body = bodyResult.value;

    // Perform additional operations here.
  }
);

Try code samples in Script Lab

Get the Script Lab for Outlook add-in and try out the item body code samples to see the get and set APIs in action. To learn more about Script Lab, see Explore Office JavaScript API using Script Lab.

See also