无法通过EWS获取Outlook 2016桌面版Outlook中插件中的电子邮件附件

时间:2016-02-17 10:09:28

标签: office365 outlook-addin exchangewebservices office365-apps

我在线程中转发的电子邮件/电子邮件中附件ID的附件ID存在问题。

从"来源/原创"提取附件时电子邮件(内联和常规附件)我可以通过询问来自Office.context.mailbox.item.attachments的附件ID来成功从EWS网络服务中检索附件内容

当我尝试从电子邮件的已发送版本获取相同的附件时,我得到了#34;指定的附件ID无效。ErrorInvalidAttachmentId0"对于电子邮件中的每个附件。如果我转发电子邮件并在发送之前为电子邮件添加额外的附件,我会获得附件的内容,仅用于"额外的"附件,不适用于任何原始附件。

只有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();

2 个答案:

答案 0 :(得分:2)

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

&#34;这是我们在Outlook中使用我们在此特定方案中计算ID的方式的错误。解决方法是进行EWS GetItem调用并以此方式获取附件ID。因此,如果您从item.attachments [i] .id获得的ID无效,您可以回退并进行EWS通话&#34;

答案 1 :(得分:1)

当我们的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()