根据Set custom header (x-header) on Outlook compose mail with JS addin帖子,我使用以下代码在OWA或Outlook 2019中撰写电子邮件时设置自定义x-header。
function addCustomHeadersAsync(classificationMarking) {
return new Office.Promise(function (resolve, reject) {
try {
/* The itemId property is not available in compose mode. If an item identifier is required,
the saveAsync method can be used to save the item to the store, which will return the item identifier in the asyncResult.value parameter in the callback function.*/
Office.context.mailbox.item.saveAsync(function (saveAsyncResult) {
/* The getCallbackTokenAsync method makes an asynchronous call to get an opaque token from the Exchange Server that hosts the user's mailbox.
The lifetime of the callback token is 5 minutes. The token is returned as a string in the asyncResult.value property.*/
Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function (getCallbackTokenAsyncResult) {
var ewsId = saveAsyncResult.value;
var token = getCallbackTokenAsyncResult.value;
var restId = Office.context.mailbox.convertToRestId(ewsId, Office.MailboxEnums.RestVersion.v2_0);
var getMessageUrl = Office.context.mailbox.restUrl + '/v2.0/me/messages/' + restId;
// The PropertyId for PS_INTERNET_HEADERS is {00020386-0000-0000-C000-000000000046}.
// https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/commonly-used-property-sets?redirectedfrom=MSDN
// https://dev59.com/75jga4cB1Zd3GeqPNami
var securityHeaders = JSON.stringify({
SingleValueExtendedProperties: [
{
PropertyId: "String {00020386-0000-0000-C000-000000000046} Name X-Custom-header",
Value: classificationMarking
}
]
});
// https://learn.microsoft.com/en-us/previous-versions/office/office-365-api/api/version-2.0/extended-properties-rest-operations#ExtendedpropertyoperationsCreateextendedpropertyinanewitem
// PATCH request is required to create an extended property in an existing item
var xhr = new XMLHttpRequest();
xhr.open('PATCH', getMessageUrl);
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer " + token);
xhr.onload = function (e) {
//console.log(this.response);
resolve();
}
xhr.send(securityHeaders);
});
});
}
catch (error) {
reject("Unable to set email custom security headers");
}
})
}
由于本地的Office 2019 + Exchange 2016仅支持API 1.5,因此我无法使用自API 1.8以来可用的新的setCustomHeaders函数(https://learn.microsoft.com/en-us/office/dev/add-ins/outlook/internet-headers)。
在https://outlook.office.com/mail/inbox和本地OWA(Exchange2016)中,一切正常。
当使用Outlook 2019(在线模式)时:
- saveAsync函数保存了草稿
- XMLHttpRequest正确设置了X-Custom-header:在尝试使用GET请求设置XMLHttpRequest之后调用Exchange 2016 REST时,Exchange正确报告了SingleValueExtendedProperties的设置
- 在此步骤之后,当手动保存或发送电子邮件时,SingleValueExtendedProperties似乎被Outlook删除或覆盖,Outlook似乎不知道已将此SingleValueExtendedProperties添加到草稿电子邮件中。
- 在Office 365上使用Oulook 2019时发现相同的行为
如何使Outlook 2019意识到通过Exchange REST API添加到草稿消息的新SingleValueExtendedProperties / x-header? 编辑11/10/2021:使用makeEwsRequestAsync进行了测试和测试摘要。
Outlook 2019 build 2108 (Office 365) |
Outlook on the web |
Outlook 2019 build 1808 (Exchange 2016) |
OWA Exchange 2016 |
|
---|---|---|---|---|
Exchange REST API XMLHttpRequest | 在Outlook中设置X-header后,发送草稿邮件时服务端正确设置了X-Custom-header,但从Outlook发送时被移除。如果在Outlook插件中设置X-header后,在OWA中打开并发送草稿,则X-Custom-header将被保留。 | OK | 在Outlook中设置X-header后,发送草稿邮件时服务端正确设置了X-Custom-header,但从Outlook发送时被移除。如果在Outlook插件中设置X-header后,在OWA中打开并发送草稿,则X-Custom-header将被保留。 | OK |
makeEwsRequestAsync() | OK | OK | 在Outlook中设置X-header后,发送草稿邮件时服务端正确设置了X-Custom-header,但从Outlook发送时被移除。如果在Outlook插件中设置X-header后,在OWA中打开并发送草稿,则X-Custom-header将被保留。 | EWS请求代理错误 |