Office-JS - Outlook插件在Outlook 2019中无法设置x-header

3

根据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(在线模式)时:
  1. saveAsync函数保存了草稿
  2. XMLHttpRequest正确设置了X-Custom-header:在尝试使用GET请求设置XMLHttpRequest之后调用Exchange 2016 REST时,Exchange正确报告了SingleValueExtendedProperties的设置
  3. 在此步骤之后,当手动保存或发送电子邮件时,SingleValueExtendedProperties似乎被Outlook删除或覆盖,Outlook似乎不知道已将此SingleValueExtendedProperties添加到草稿电子邮件中。
  4. 在Office 365上使用Oulook 2019时发现相同的行为
这段代码是否正确,以便使用SingleValueExtendedProperties在Outlook 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请求代理错误

你能提供你正在使用的确切构建版本号吗? - Outlook Add-ins Team - MSFT
我正在使用office-js 1.1.70{ "name": "@microsoft/office-js", "version": "1.1.70", ... }Addin必须与Office 2019 VL配合使用。 - kjack51
请澄清一下,您可以分享一下您的Outlook 2019版本号吗? - Outlook Add-ins Team - MSFT
Office 2019 1808 VL build 10378.20029 - kjack51
1个回答

1
您正在尝试的内容在Win32 Outlook客户端上不可能实现。 您的第二步(XMLHttpRequest)有效地创建了该项的两个版本,一个在客户端上,另一个在服务器上。 当该项最终发送时,其中一个将覆盖另一个(最有可能是从客户端发送的那个),并覆盖您所做的更改。
1.8中的setCustomHeaders是为解决此问题而创建的。 实际上,setCustomHeaders不依赖于服务器进行其功能,因此只要您的客户端支持1.8,它就应该可以工作。
Office 2019(零售版)支持1.8。 Office 2019(批量许可证)不支持。

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

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


我理解XMLHttpRequest问题在零售版(Office 365)和VL版本中都是一致的,但为什么在尝试使用makeEwsRequestAsync()设置互联网标头时,在Outlook 2019 build 1808上失败了?使用makeEwsRequestAsync(),Outlook知道updateitem操作,并应该像在build 2108中一样更新草稿,不是吗?测试中使用了https://github.com/davecra/easyEWS/blob/master/easyEws.js的updateEwsHeader函数。 - kjack51
根据先前问题的答案,是否有计划在Office 2019 VL版本1808中支持API 1.8的回溯? - kjack51
目前没有计划将1.8回溯到2019 VL。Office 2021是具有该支持的VL版本。 - Outlook Add-ins Team - MSFT
makeEwsRequestAsync() 请求直接发送到服务器。(客户端不知道请求的内容,只是将其转发到服务器)。我不确定为什么在2108版本中它能够正常工作。这可能是由于时间/服务器问题而偶尔成功。支持进行此操作的方式是使用1.8 API。 - Outlook Add-ins Team - MSFT

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接