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 or the new Outlook on Windows

In Outlook on the web and the new Outlook on Windows, 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.

When you call Office.context.mailbox.item.body.getAsync on a message reply, the entire body of a conversation thread is returned. If you want the returned body to reflect the user's Message Organization setting, you can specify the bodyMode option in the getAsync call. The following table lists the portion of the body returned depending on the bodyMode configuration.

bodyMode configuration Effect on body
bodyMode isn't specified in the getAsync call The entire body of the conversation thread is returned.
bodyMode is set to Office.MailboxEnums.BodyMode.FullBody The entire body of the conversation thread is returned.
bodyMode is set to Office.MailboxEnums.BodyMode.HostConfig 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.

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.

Note

The bodyMode option is ignored in Outlook on Windows (classic), on Mac, and on mobile devices.

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