Office-JS - Outlook 插件未在 outlook 2019 中设置 x-headers

Office-JS - Outlook addin not setting x-headers in outlook 2019

基于post,我在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://docs.microsoft.com/en-us/office/client-developer/outlook/mapi/commonly-used-property-sets?redirectedfrom=MSDN
                    // 
                    var securityHeaders = JSON.stringify({
                        SingleValueExtendedProperties: [
                            {
                                PropertyId: "String {00020386-0000-0000-C000-000000000046} Name X-Custom-header",
                                Value: classificationMarking
                            }
                        ]
                    });

                    // https://docs.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");
        }
    })
}

由于on-premisesOffice 2019 + Exchange 2016仅支持API1.5,我无法使用新的setCustomHeaders功能因为 API 1.8 (https://docs.microsoft.com/en-us/office/dev/add-ins/outlook/internet-headers)

https://outlook.office.com/mail/inbox & on-premises OWA (Exchange2016) 中一切正常。

使用 Outlook 2019 时(在线模式):

  1. saveAsync 函数保存草稿
  2. XMLHttpRequest 正确设置了 X-Custom-header:在尝试使用 GET 请求设置 XMLHttpRequest 之后调用 Exchange 2016 REST 时,Exchange 正确报告了 SingleValueExtendedProperties beeing set
  3. 在此步骤之后,当手动保存或发送电子邮件时,SingleValueExtendedProperties 似乎被 Outlook 删除或覆盖,而 Outlook 似乎并不知道此 SingleValueExtendedProperties 已添加到电子邮件草稿中。
  4. Office 365 上的 Oulook 2019 发现了相同的行为

此代码是否正确设置自定义电子邮件 headers 使用 Outlook 2019 的 SingleValueExtendedProperties?

如何让 Outlook 2019 知道通过 Exchange REST API 添加到草稿消息的新 SingleValueExtendedProperties / x-header?

编辑 2021 年 11 月 10 日:使用 makeEwsRequestAsync 和测试摘要进行测试

Outlook 2019
build 2108 (Office 365)
Outlook
on the web
Outlook 2019
build 1808 (Exchange 2016)
OWA
Exchange 2016
Exchange REST API XMLHttpRequest X-Custom-header correctly set server side but removed when sending the draft email from Outlook. If opening and sending the draft from OWA after having set the X-header from the addin in Outlook, X-Custom-header is preserved OK X-Custom-header correctly set server side but removed when sending the draft email from Outlook. If opening and sending the draft from OWA after having set the X-header from the addin in Outlook, X-Custom-header is preserved OK
makeEwsRequestAsync() OK OK X-Custom-header correctly set server side but removed when sending the draft email from Outlook. If opening and sending the draft from OWA after having set the X-header from the addin in Outlook, X-Custom-header is preserved EWS request proxy error

您尝试执行的操作在 Win32 Outlook 客户端上无法执行。您的第 2 步 (XMLHttpRequest) 有效地创建了项目的两个版本,一个在客户端上,一个在服务器上。当项目最终发送时,一个将覆盖另一个(很可能是从客户端发送的那个),并覆盖您所做的更改。

1.8 中的 setCustomHeaders 就是为了解决这个问题而创建的。 setCustomHeaders,实际上它的功能并不依赖于服务器,因此只要您的客户端支持 1.8,它应该 可以工作。

Office 2019(零售版)确实支持 1.8。 Office 2019(批量许可)没有。

https://docs.microsoft.com/en-us/office/dev/add-ins/reference/requirement-sets/outlook-api-requirement-sets

批量许可版本的用户需要升级才能获得此支持。