Office.MessageCompose interface
The message compose mode of Office.context.mailbox.item.
Important:
This is an internal Outlook object, not directly exposed through existing interfaces. You should treat this as a mode of
Office.context.mailbox.item
. For more information, refer to the Object Model page.When calling
Office.context.mailbox.item
on a message, note that the Reading Pane in the Outlook client must be turned on. For guidance on how to configure the Reading Pane, see Use and configure the Reading Pane to preview messages.
Parent interfaces:
- Extends
Properties
bcc | Gets an object that provides methods to get or update the recipients on the Bcc (blind carbon copy) line of a message. Depending on the client/platform (i.e., Windows, Mac, etc.), limits may apply on how many recipients you can get or update. See the Recipients object for more details. |
body | Gets an object that provides methods for manipulating the body of an item. |
cc | Provides access to the Cc (carbon copy) recipients of a message. The type of object and level of access depend on the mode of the current item. The |
conversation |
Gets an identifier for the email conversation that contains a particular message. You can get an integer for this property if your mail app is activated in read forms or responses in compose forms. If subsequently the user changes the subject of the reply message, upon sending the reply, the conversation ID for that message will change and that value you obtained earlier will no longer apply. You get null for this property for a new item in a compose form. If the user sets a subject and saves the item, the |
item |
Gets the type of item that an instance represents. The |
subject | Gets or sets the description that appears in the subject field of an item. The The |
to | Provides access to the recipients on the To line of a message. The type of object and level of access depend on the mode of the current item. The |
Methods
add |
Adds a file to a message or appointment as an attachment. The |
add |
Adds a file to a message or appointment as an attachment. The |
add |
Adds an Exchange item, such as a message, as an attachment to the message or appointment. The You can subsequently use the identifier with the If your Office Add-in is running in Outlook on the web or new Outlook on Windows, the |
add |
Adds an Exchange item, such as a message, as an attachment to the message or appointment. The You can subsequently use the identifier with the If your Office Add-in is running in Outlook on the web or new Outlook on Windows, the |
load |
Asynchronously loads custom properties for this add-in on the selected item. Custom properties are stored as key-value pairs on a per-app, per-item basis. This method returns a CustomProperties object in the callback, which provides methods to access the custom properties specific to the current item and the current add-in. Custom properties aren't encrypted on the item, so this shouldn't be used as secure storage. The custom properties are provided as a |
remove |
Removes an attachment from a message or appointment. The |
remove |
Removes an attachment from a message or appointment. The |
Property Details
bcc
Gets an object that provides methods to get or update the recipients on the Bcc (blind carbon copy) line of a message.
Depending on the client/platform (i.e., Windows, Mac, etc.), limits may apply on how many recipients you can get or update. See the Recipients object for more details.
bcc: Recipients;
Property Value
Remarks
Minimum permission level: read item
Applicable Outlook mode: Message Compose
Examples
Office.context.mailbox.item.bcc.setAsync( ['alice@contoso.com', 'bob@contoso.com'] );
Office.context.mailbox.item.bcc.addAsync( ['jason@contoso.com'] );
Office.context.mailbox.item.bcc.getAsync(callback);
function callback(asyncResult) {
const arrayOfBccRecipients = asyncResult.value;
}
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/30-recipients-and-attendees/get-set-bcc-message-compose.yaml
Office.context.mailbox.item.bcc.getAsync(function(asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
const msgBcc = asyncResult.value;
console.log("Message being blind-copied to:");
for (let i = 0; i < msgBcc.length; i++) {
console.log(msgBcc[i].displayName + " (" + msgBcc[i].emailAddress + ")");
}
} else {
console.error(asyncResult.error);
}
});
...
const email = $("#emailBcc")
.val()
.toString();
const emailArray = [email];
Office.context.mailbox.item.bcc.setAsync(emailArray, function(asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
console.log("Succeeded in setting Bcc field.");
} else {
console.error(asyncResult.error);
}
});
body
Gets an object that provides methods for manipulating the body of an item.
body: Body;
Property Value
Remarks
Minimum permission level: read item
Applicable Outlook mode: Message Compose
Examples
// This example gets the body of the item as plain text.
Office.context.mailbox.item.body.getAsync(
"text",
{ asyncContext: "This is passed to the callback" },
function callback(result) {
// Do something with the result.
});
// The following is an example of the result parameter passed to the callback function.
{
"value": "TEXT of whole body (including threads below)",
"status": "succeeded",
"asyncContext": "This is passed to the callback"
}
cc
Provides access to the Cc (carbon copy) recipients of a message. The type of object and level of access depend on the mode of the current item.
The cc
property returns a Recipients
object that provides methods to get or update the recipients on the Cc line of the message. However, depending on the client/platform (i.e., Windows, Mac, etc.), limits may apply on how many recipients you can get or update. See the Recipients object for more details.
cc: Recipients;
Property Value
Remarks
Minimum permission level: read item
Applicable Outlook mode: Message Compose
Examples
Office.context.mailbox.item.cc.setAsync( ['alice@contoso.com', 'bob@contoso.com'] );
Office.context.mailbox.item.cc.addAsync( ['jason@contoso.com'] );
Office.context.mailbox.item.cc.getAsync(callback);
function callback(asyncResult) {
const arrayOfCcRecipients = asyncResult.value;
}
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/30-recipients-and-attendees/get-set-cc-message-compose.yaml
Office.context.mailbox.item.cc.getAsync(function(asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
const msgCc = asyncResult.value;
console.log("Message being copied to:");
for (let i = 0; i < msgCc.length; i++) {
console.log(msgCc[i].displayName + " (" + msgCc[i].emailAddress + ")");
}
} else {
console.error(asyncResult.error);
}
});
...
const email = $("#emailCc")
.val()
.toString();
const emailArray = [email];
Office.context.mailbox.item.cc.setAsync(emailArray, function(asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
console.log("Succeeded in setting Cc field.");
} else {
console.error(asyncResult.error);
}
});
conversationId
Gets an identifier for the email conversation that contains a particular message.
You can get an integer for this property if your mail app is activated in read forms or responses in compose forms. If subsequently the user changes the subject of the reply message, upon sending the reply, the conversation ID for that message will change and that value you obtained earlier will no longer apply.
You get null for this property for a new item in a compose form. If the user sets a subject and saves the item, the conversationId
property will return a value.
conversationId: string;
Property Value
string
Remarks
Minimum permission level: read item
Applicable Outlook mode: Message Compose
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/90-other-item-apis/get-conversation-id-message.yaml
console.log(`Conversation ID: ${Office.context.mailbox.item.conversationId}`);
itemType
Gets the type of item that an instance represents.
The itemType
property returns one of the ItemType
enumeration values, indicating whether the item object instance is a message or an appointment.
itemType: MailboxEnums.ItemType | string;
Property Value
Office.MailboxEnums.ItemType | string
Remarks
Minimum permission level: read item
Applicable Outlook mode: Message Compose
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/90-other-item-apis/get-item-type.yaml
const itemType = Office.context.mailbox.item.itemType;
switch (itemType) {
case Office.MailboxEnums.ItemType.Appointment:
console.log(`Current item is an ${itemType}.`);
break;
case Office.MailboxEnums.ItemType.Message:
console.log(`Current item is a ${itemType}. A message could be an email, meeting request, meeting response, or meeting cancellation.`);
break;
}
subject
Gets or sets the description that appears in the subject field of an item.
The subject
property gets or sets the entire subject of the item, as sent by the email server.
The subject
property returns a Subject
object that provides methods to get and set the subject.
subject: Subject;
Property Value
Remarks
Minimum permission level: read item
Applicable Outlook mode: Message Compose
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/90-other-item-apis/get-set-subject-compose.yaml
Office.context.mailbox.item.subject.getAsync((result) => {
if (result.status !== Office.AsyncResultStatus.Succeeded) {
console.error(`Action failed with message ${result.error.message}`);
return;
}
console.log(`Subject: ${result.value}`);
});
...
let subject = "Hello World!";
Office.context.mailbox.item.subject.setAsync(subject, (result) => {
if (result.status !== Office.AsyncResultStatus.Succeeded) {
console.error(`Action failed with message ${result.error.message}`);
return;
}
console.log(`Successfully set subject to ${subject}`);
});
to
Provides access to the recipients on the To line of a message. The type of object and level of access depend on the mode of the current item.
The to
property returns a Recipients
object that provides methods to get or update the recipients on the To line of the message. However, depending on the client/platform (i.e., Windows, Mac, etc.), limits may apply on how many recipients you can get or update. See the Recipients object for more details.
to: Recipients;
Property Value
Remarks
Minimum permission level: read item
Applicable Outlook mode: Message Compose
Examples
Office.context.mailbox.item.to.setAsync( ['alice@contoso.com', 'bob@contoso.com'] );
Office.context.mailbox.item.to.addAsync( ['jason@contoso.com'] );
Office.context.mailbox.item.to.getAsync(callback);
function callback(asyncResult) {
const arrayOfToRecipients = asyncResult.value;
}
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/30-recipients-and-attendees/get-set-to-message-compose.yaml
Office.context.mailbox.item.to.getAsync(function(asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
const msgTo = asyncResult.value;
console.log("Message being sent to:");
for (let i = 0; i < msgTo.length; i++) {
console.log(msgTo[i].displayName + " (" + msgTo[i].emailAddress + ")");
}
} else {
console.error(asyncResult.error);
}
});
...
const email = $("#emailTo")
.val()
.toString();
const emailArray = [email];
Office.context.mailbox.item.to.setAsync(emailArray, function(asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
console.log("Succeeded in setting To field.");
} else {
console.error(asyncResult.error);
}
});
Method Details
addFileAttachmentAsync(uri, attachmentName, options, callback)
Adds a file to a message or appointment as an attachment.
The addFileAttachmentAsync
method uploads the file at the specified URI and attaches it to the item in the compose form.
addFileAttachmentAsync(uri: string, attachmentName: string, options: Office.AsyncContextOptions & { isInline: boolean }, callback?: (asyncResult: Office.AsyncResult<string>) => void): void;
Parameters
- uri
-
string
The URI that provides the location of the file to attach to the message or appointment. The maximum length is 2048 characters.
- attachmentName
-
string
The name of the attachment that is shown while the attachment is uploading. The maximum length is 255 characters.
- options
-
Office.AsyncContextOptions & { isInline: boolean }
An object literal that contains one or more of the following properties:- asyncContext
: Developers can provide any object they wish to access in the callback function. isInline
: If true, indicates that the attachment will be shown inline as an image in the message body and won't be displayed in the attachment list.
- callback
-
(asyncResult: Office.AsyncResult<string>) => void
Optional. When the method completes, the function passed in the callback
parameter is called with a single parameter of type Office.AsyncResult
. On success, the attachment identifier will be provided in the asyncResult.value
property. If uploading the attachment fails, the asyncResult
object will contain an Error
object that provides a description of the error.
Returns
void
Remarks
Minimum permission level: read/write item
Applicable Outlook mode: Message Compose
Important:
In recent builds of classic Outlook on Windows, a bug was introduced that incorrectly appends an
Authorization: Bearer
header to this action (whether using this API or the Outlook UI). To work around this issue, you can try using theaddFileAttachmentFromBase64
API introduced with requirement set 1.8.The URI of the file to be attached must support caching in production. The server hosting the image shouldn't return a
Cache-Control
header that specifiesno-cache
,no-store
, or similar options in the HTTP response. However, when you're developing the add-in and making changes to files, caching can prevent you from seeing your changes. We recommend usingCache-Control
headers during development.You can use the same URI with the
removeAttachmentAsync
method to remove the attachment in the same session.
Errors:
AttachmentSizeExceeded
: The attachment is larger than allowed.FileTypeNotSupported
: The attachment has an extension that is not allowed.NumberOfAttachmentsExceeded
: The message or appointment has too many attachments.
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/40-attachments/attachments-compose.yaml
const attachmentUrl = $("#attachmentUrl")
.val()
.toString();
Office.context.mailbox.item.addFileAttachmentAsync(
attachmentUrl,
getFileName(attachmentUrl),
{ isInline: false },
(result) => {
console.log(result);
}
);
addFileAttachmentAsync(uri, attachmentName, callback)
Adds a file to a message or appointment as an attachment.
The addFileAttachmentAsync
method uploads the file at the specified URI and attaches it to the item in the compose form.
addFileAttachmentAsync(uri: string, attachmentName: string, callback?: (asyncResult: Office.AsyncResult<string>) => void): void;
Parameters
- uri
-
string
The URI that provides the location of the file to attach to the message or appointment. The maximum length is 2048 characters.
- attachmentName
-
string
The name of the attachment that is shown while the attachment is uploading. The maximum length is 255 characters.
- callback
-
(asyncResult: Office.AsyncResult<string>) => void
Optional. When the method completes, the function passed in the callback
parameter is called with a single parameter of type Office.AsyncResult
. On success, the attachment identifier will be provided in the asyncResult.value
property. If uploading the attachment fails, the asyncResult
object will contain an Error
object that provides a description of the error.
Returns
void
Remarks
Minimum permission level: read/write item
Applicable Outlook mode: Message Compose
Important:
In recent builds of classic Outlook on Windows, a bug was introduced that incorrectly appends an
Authorization: Bearer
header to this action (whether using this API or the Outlook UI). To work around this issue, you can try using theaddFileAttachmentFromBase64
API introduced with requirement set 1.8.The URI of the file to be attached must support caching in production. The server hosting the image shouldn't return a
Cache-Control
header that specifiesno-cache
,no-store
, or similar options in the HTTP response. However, when you're developing the add-in and making changes to files, caching can prevent you from seeing your changes. We recommend usingCache-Control
headers during development.You can use the same URI with the
removeAttachmentAsync
method to remove the attachment in the same session.
Errors:
AttachmentSizeExceeded
: The attachment is larger than allowed.FileTypeNotSupported
: The attachment has an extension that is not allowed.NumberOfAttachmentsExceeded
: The message or appointment has too many attachments.
addItemAttachmentAsync(itemId, attachmentName, options, callback)
Adds an Exchange item, such as a message, as an attachment to the message or appointment.
The addItemAttachmentAsync
method attaches the item with the specified Exchange identifier to the item in the compose form. If you specify a callback function, the method is called with one parameter, asyncResult
, which contains either the attachment identifier or a code that indicates any error that occurred while attaching the item. You can use the options parameter to pass state information to the callback function, if needed.
You can subsequently use the identifier with the removeAttachmentAsync
method to remove the attachment in the same session.
If your Office Add-in is running in Outlook on the web or new Outlook on Windows, the addItemAttachmentAsync
method can attach items to items other than the item that you are editing. However, this isn't supported and isn't recommended.
addItemAttachmentAsync(itemId: any, attachmentName: string, options: Office.AsyncContextOptions, callback?: (asyncResult: Office.AsyncResult<string>) => void): void;
Parameters
- itemId
-
any
The Exchange identifier of the item to attach. The maximum length is 100 characters.
- attachmentName
-
string
The name of the attachment that is shown while the attachment is uploading. The maximum length is 255 characters.
- options
- Office.AsyncContextOptions
An object literal that contains one or more of the following properties:- asyncContext
: Developers can provide any object they wish to access in the callback function.
- callback
-
(asyncResult: Office.AsyncResult<string>) => void
Optional. When the method completes, the function passed in the callback
parameter is called with a single parameter of type Office.AsyncResult
. On success, the attachment identifier will be provided in the asyncResult.value
property. If adding the attachment fails, the asyncResult
object will contain an Error
object that provides a description of the error.
Returns
void
Remarks
Minimum permission level: read/write item
Applicable Outlook mode: Message Compose
Errors:
NumberOfAttachmentsExceeded
: The message or appointment has too many attachments.
Examples
// The following example adds an existing Outlook item as an attachment
// with the name "My Attachment".
function addAttachment() {
// EWS ID of item to attach (shortened for readability).
const itemId = "AAMkADI1...AAA=";
// The values in asyncContext can be accessed in the callback.
const options = { asyncContext: { var1: 1, var2: 2 } };
Office.context.mailbox.item.addItemAttachmentAsync(itemId, "My Attachment", options, (result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.error("Failed to add attachment: " + result.error.message);
return;
}
console.log("Attachment added successfully.");
console.log("var1: " + result.asyncContext.var1);
console.log("var2: " + result.asyncContext.var2);
});
}
addItemAttachmentAsync(itemId, attachmentName, callback)
Adds an Exchange item, such as a message, as an attachment to the message or appointment.
The addItemAttachmentAsync
method attaches the item with the specified Exchange identifier to the item in the compose form. If you specify a callback function, the method is called with one parameter, asyncResult
, which contains either the attachment identifier or a code that indicates any error that occurred while attaching the item. You can use the options parameter to pass state information to the callback function, if needed.
You can subsequently use the identifier with the removeAttachmentAsync
method to remove the attachment in the same session.
If your Office Add-in is running in Outlook on the web or new Outlook on Windows, the addItemAttachmentAsync
method can attach items to items other than the item that you are editing. However, this isn't supported and isn't recommended.
addItemAttachmentAsync(itemId: any, attachmentName: string, callback?: (asyncResult: Office.AsyncResult<string>) => void): void;
Parameters
- itemId
-
any
The Exchange identifier of the item to attach. The maximum length is 100 characters.
- attachmentName
-
string
The name of the attachment that is shown while the attachment is uploading. The maximum length is 255 characters.
- callback
-
(asyncResult: Office.AsyncResult<string>) => void
Optional. When the method completes, the function passed in the callback
parameter is called with a single parameter of type Office.AsyncResult
. On success, the attachment identifier will be provided in the asyncResult.value
property. If adding the attachment fails, the asyncResult
object will contain an Error
object that provides a description of the error.
Returns
void
Remarks
Minimum permission level: read/write item
Applicable Outlook mode: Message Compose
Errors:
NumberOfAttachmentsExceeded
: The message or appointment has too many attachments.
loadCustomPropertiesAsync(callback, userContext)
Asynchronously loads custom properties for this add-in on the selected item.
Custom properties are stored as key-value pairs on a per-app, per-item basis. This method returns a CustomProperties object in the callback, which provides methods to access the custom properties specific to the current item and the current add-in. Custom properties aren't encrypted on the item, so this shouldn't be used as secure storage.
The custom properties are provided as a CustomProperties
object in the asyncResult.value
property. This object can be used to get, set, save, and remove custom properties from the mail item.
loadCustomPropertiesAsync(callback: (asyncResult: Office.AsyncResult<CustomProperties>) => void, userContext?: any): void;
Parameters
- callback
-
(asyncResult: Office.AsyncResult<Office.CustomProperties>) => void
When the method completes, the function passed in the callback
parameter is called with a single parameter of type Office.AsyncResult
.
- userContext
-
any
Optional. Developers can provide any object they wish to access in the callback function. This object can be accessed by the asyncResult.asyncContext
property in the callback function.
Returns
void
Remarks
To learn more about custom properties, see Get and set add-in metadata for an Outlook add-in.
Minimum permission level: read item
Applicable Outlook mode: Message Compose
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/15-item-custom-properties/load-set-get-save.yaml
Office.context.mailbox.item.loadCustomPropertiesAsync((result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.error(`loadCustomPropertiesAsync failed with message ${result.error.message}`);
return;
}
customProps = result.value;
console.log("Loaded the CustomProperties object.");
});
removeAttachmentAsync(attachmentId, options, callback)
Removes an attachment from a message or appointment.
The removeAttachmentAsync
method removes the attachment with the specified identifier from the item. As a best practice, you should use the attachment identifier to remove an attachment only if the same mail app has added that attachment in the same session. In Outlook on the web, on mobile devices, and in new Outlook on Windows, the attachment identifier is valid only within the same session. A session is over when the user closes the app, or if the user starts composing an inline form then subsequently pops out the form to continue in a separate window.
removeAttachmentAsync(attachmentId: string, options: Office.AsyncContextOptions, callback?: (asyncResult: Office.AsyncResult<void>) => void): void;
Parameters
- attachmentId
-
string
The identifier of the attachment to remove. The maximum string length of the attachmentId
is 200 characters in Outlook on the web and on Windows (new and classic).
- options
- Office.AsyncContextOptions
An object literal that contains one or more of the following properties:- asyncContext
: Developers can provide any object they wish to access in the callback function.
- callback
-
(asyncResult: Office.AsyncResult<void>) => void
Optional. When the method completes, the function passed in the callback
parameter is called with a single parameter of type Office.AsyncResult
. If removing the attachment fails, the asyncResult.error
property will contain an error code with the reason for the failure.
Returns
void
Remarks
Minimum permission level: read/write item
Applicable Outlook mode: Message Compose
Important*: The removeAttachmentAsync
method doesn't remove inline attachments from a mail item. To remove an inline attachment, first get the item's body, then remove any references of the attachment from its contents. Use the Office.Body APIs to get and set the body of an item.
Errors:
InvalidAttachmentId
: The attachment identifier does not exist.
Examples
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/outlook/40-attachments/attachments-compose.yaml
Office.context.mailbox.item.removeAttachmentAsync(
$("#attachmentId")
.val()
.toString(),
(result) => {
if (result.status === Office.AsyncResultStatus.Failed) {
console.error(result.error.message);
return;
}
console.log(`Attachment removed successfully.`);
}
);
removeAttachmentAsync(attachmentId, callback)
Removes an attachment from a message or appointment.
The removeAttachmentAsync
method removes the attachment with the specified identifier from the item. As a best practice, you should use the attachment identifier to remove an attachment only if the same mail app has added that attachment in the same session. In Outlook on the web, on mobile devices, and in new Outlook on Windows, the attachment identifier is valid only within the same session. A session is over when the user closes the app, or if the user starts composing an inline form then subsequently pops out the form to continue in a separate window.
removeAttachmentAsync(attachmentId: string, callback?: (asyncResult: Office.AsyncResult<void>) => void): void;
Parameters
- attachmentId
-
string
The identifier of the attachment to remove. The maximum string length of the attachmentId
is 200 characters in Outlook on the web and on Windows (new and classic).
- callback
-
(asyncResult: Office.AsyncResult<void>) => void
Optional. When the method completes, the function passed in the callback
parameter is called with a single parameter of type Office.AsyncResult
. If removing the attachment fails, the asyncResult.error
property will contain an error code with the reason for the failure.
Returns
void
Remarks
Minimum permission level: read/write item
Applicable Outlook mode: Message Compose
Important*: The removeAttachmentAsync
method doesn't remove inline attachments from a mail item. To remove an inline attachment, first get the item's body, then remove any references of the attachment from its contents. Use the Office.Body APIs to get and set the body of an item.
Errors:
InvalidAttachmentId
: The attachment identifier does not exist.
Office Add-ins