无法从 Outlook 2016 桌面版中的 Outlook 插件中的转发电子邮件的 EWS 获取附件

Can't get attachments from EWS for fowarded emails in an Outlook addin in the desktop version of Outlook 2016

我在线程中转发 emails/emails 的附件的附件 ID 有问题。

从 "source/original" 电子邮件中提取附件(内联附件和普通附件)时,我可以通过从 Office.context.mailbox.item.attachments[=12] 请求附件 ID 成功地从 EWS 网络服务检索附件的内容=]

当我尝试从转发版本的电子邮件中获取相同的附件时,我收到了电子邮件中每个附件的 "The specified attachment Id is invalid.ErrorInvalidAttachmentId0"。如果我转发一封电子邮件并在发送前向电子邮件添加一个额外的附件,我会得到 "extra" 附件的附件内容,而不是任何原始附件的内容。

该错误仅发生在 Outlook 桌面客户端上。 (版本 16.0.6366.2062)。使用 chrome 或 Internet Explorer 时,OWA 中不存在此问题。

这是我的 API 用来调用 EWS 的代码。

string getAttachmentRequest =
    @"<?xml version=""1.0"" encoding=""utf-8""?>
    <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
    xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
    xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""
    xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
    <soap:Header>
    <t:RequestServerVersion Version=""Exchange2013"" />
    </soap:Header>
        <soap:Body>
        <GetAttachment xmlns=""http://schemas.microsoft.com/exchange/services/2006/messages""
        xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
            <AttachmentShape/>
            <AttachmentIds>
            <t:AttachmentId Id=""{0}""/>
            </AttachmentIds>
        </GetAttachment>
        </soap:Body>
    </soap:Envelope>";
getAttachmentRequest = String.Format(getAttachmentRequest, attachmentId);

// Prepare a web request object.
HttpWebRequest webRequest = WebRequest.CreateHttp(ewsUrl);
webRequest.Headers.Add("Authorization", string.Format("Bearer {0}", authToken));
webRequest.PreAuthenticate = true;
webRequest.AllowAutoRedirect = false;
webRequest.Method = "POST";
webRequest.ContentType = "text/xml; charset=utf-8";

// Construct the SOAP message for the GetAttchment operation.
byte[] bodyBytes = System.Text.Encoding.UTF8.GetBytes(getAttachmentRequest);
webRequest.ContentLength = bodyBytes.Length;

Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(bodyBytes, 0, bodyBytes.Length);
requestStream.Close();

// Make the request to the Exchange server and get the response.
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

Andrew Salamatov 在 yammer 上回答了这个问题:

"It's a bug that we have in Outlook with the way we calculate the id in this specific scenario. The workaround would be to make an EWS GetItem call and get the attachment id that way. So if the id you got from item.attachments[i].id doesn't work, you could fallback and make an EWS call"

当我们的 EWS 服务器升级到 2016 时,我遇到了同样的问题,我之前使用 PowerShell 中的 EWS Managed API 通过以下方式下载附件电子邮件:

# load the email items.
$fiItems = $exchService.FindItems( $Inbox.Id, $email_filter, $ivItemView)  

# create the Attachment properties object
$attachment_PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet(
        [Microsoft.Exchange.WebServices.Data.ItemSchema]::Attachments)  

# LOAD THE ATTACHMENT AS AN EWS ITEM
$attached_email = $email_from_server.attachments[0]
$attached_email.load($attachment_PropertySet)

显然,对于新的 EWS 服务器,$attachment_PropertySet 参数会导致错误,修复方法是简单地删除它,即:

$attached_email.load()