【问题标题】:Unable to add attachment to Office JS Add-in无法将附件添加到 Office JS 加载项
【发布时间】:2019-03-22 06:29:18
【问题描述】:

我创建了一个附加电子邮件的 EWS 请求,但我从中收到空值,但状态为“成功”。 顺便说一句。

我首先创建了一个将电子邮件保存到草稿的 makeEwsRequestAsync 请求,它已经可以使用,但是当我尝试使用此请求向它添加附件时,它没有添加。任何建议或帮助请

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
function createAttachment() {

    var request =
        '<?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:Body>' +
        '    <CreateAttachment xmlns="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
        '      <ParentItemId Id="'+itemID+'" />' +
        '      <Attachments>' +
        '        <t:ItemAttachment>' +
        '          <t:Name>Please</t:Name>' +
        '           <t:Message>' +
        '               <t:ItemClass>IPM>Note</t:ItemClass>' +
        '               <t:Subject>test</t:Subject>' +
        '               <t:Body BodyType="Text">my test</t:Body>' +
        '           </t:Message>' +
        '        </t:ItemAttachment>' +
        '      </Attachments>' +
        '    </CreateAttachment>' +
        '  </soap:Body>' +
        '</soap:Envelope>';
    return request;
}

</script>

这是我的创建附件 EWS 请求

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">


    function composeMail(emailSubject, emailDescription) {
        var subject= subjectPrefix + " " + emailSubject;
        var request =
            '<?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:m="http://schemas.microsoft.com/exchange/services/2006/messages"' +
            '               xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"' +
            '               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
            '  <soap:Header>' +
            '    <RequestServerVersion Version="Exchange2007_SP1" />' +
            '  </soap:Header>' +
            '  <soap:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
            '    <m:CreateItem MessageDisposition="SaveOnly">' +
            '      <m:SavedItemFolderId>' +
            '        <t:DistinguishedFolderId Id="drafts" />' +
            '      </m:SavedItemFolderId>' +
            '      <m:Items>' +
            '            <t:Message>' +
            '              <t:ItemClass>IPM.Note</t:ItemClass>' +
            '              <t:Subject>' + subject + '</t:Subject>' +
            '              <t:Body BodyType="HTML">' + emailDescription + '</t:Body>' +
            '              <t:Importance>Low</t:Importance>' +
            '              <t:ToRecipients>' +
            '                <t:Mailbox>' +
            '                  <t:EmailAddress>' + recepient + '</t:EmailAddress>' +
            '                </t:Mailbox>' +
            '              </t:ToRecipients>' +
            '              <t:IsRead>false</t:IsRead>' +
            '            </t:Message>' +
            '          </m:Items>' +
            '        </m:CreateItem>' +
            '  </soap:Body>' +
            '</soap:Envelope>';
        return request;
    }
</script>

这是我的创建电子邮件 EWS 请求

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
    function send() {
        var request =
            '<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:Body>' +
            '    <SendItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"' +
            '    SaveItemToFolder="true">' +
            '      <ItemIds>' +
            '        <t:ItemId Id="' + itemID + '"/>' +
            '      </ItemIds>' +
            '    </SendItem>' +
            '  </soap:Body>' +
            '</soap:Envelope>';
        return request;
    }

</script>

这是我发送 EWS 请求,我正在使用

呼叫他们

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
    function sendRequest(emailSubject, emailDescription, emailItemID) {
        // Create a local variable that contains the mailbox.
        try {
            itemID = Office.context.mailbox.item.itemId;
            Office.context.mailbox.makeEwsRequestAsync(
                composeMail(emailSubject, emailDescription), callbackCompose);
            Office.context.mailbox.makeEwsRequestAsync(
                createAttachment(), callbackAttachment);
            Office.context.mailbox.makeEwsRequestAsync(
                send(), callbackSend);
        } catch (error) {
            $("#id-error-msg").text(error);
        }
</script>

【问题讨论】:

  • 您能否使用附件 API 创建附件而不是 makeEwsRequest? addFileAttachmentAsync。您使用 makeEwsRequest 是否有特殊原因?
  • 即使在电子邮件的阅读模式下我也需要发送电子邮件,但根据我已阅读和尝试的内容,addFileAttachmentAsync 仅在用户处于撰写模式时才有效。所以我切换到 EWS 请求。请让我知道它是否可能。提前致谢!
  • makeEwsRequestAsync 中支持的方法是有限的。见docs.microsoft.com/en-us/outlook/add-ins/web-services。特别是,通过 makeEwsRequestAsync 不支持 CreateAttachment。要发送新电子邮件,您是否考虑过使用图形 API? docs.microsoft.com/en-us/outlook/add-ins/use-rest-api
  • 感谢您的及时回复:)。顺便提一句。我能够让它工作,但需要我修改很多。首先,我删除了 2 个 EWS 请求(发送和创建附件),然后我在创建项目请求中使用 Mime 添加了附件。我可以使用对 Exchange Server 的请求来获取 MimeContent。
  • 另外,关于图形 API。我尝试使用 restUrl 但不知何故它在 Outlook for desktop 中返回 undefined 但在 Outlook web 中工作正常。谢谢!

标签: outlook exchangewebservices outlook-addin office-js add-in


【解决方案1】:

在我看来像糟糕的 XML,例如

'<t:ItemClass>IPM>Note</t:ItemClass>' +

将在服务器上产生验证错误(您应该在来自服务器的 EWS 响应中看到,例如您的请求应该是

'<t:ItemClass>IPM.Note</t:ItemClass>'

【讨论】:

  • 我可以使用其他方法吗?
  • 您是否尝试查看正在返回的 EWS 操作的结果?他们会告诉你第一个错误正在发生,所以听起来你没有使用结果来诊断问题
  • 好的,现在我在响应中看到了错误,它给了我这条消息“请求的 Web 方法对此调用者或应用程序不可用。”带有故障代码“ErrorAccessDenied”。这是我第一次尝试这个。提前致谢
  • 您能否说明是哪个 EWS 请求返回了错误和故障代码?您使用哪个平台进行测试,Outlook Web、Outlook for Windows 等?
  • 嗨!我在 xml 中添加 对象时返回错误“ErrorAccessDenied”。但是当我删除它时。它运行良好并保存在草稿文件夹中。提前致谢
【解决方案2】:

当您想在撰写模式下操作电子邮件时,您可以参考 Microsoft 团队的此文档。我相信它更加简单和全面。

-https://docs.microsoft.com/en-us/outlook/add-ins/add-and-remove-attachments-to-an-item-in-a-compose-form

否则,如果您想从阅读模式处理电子邮件。可以有两种选择。

一种是使用 Rest API。但是,您需要满足一些要求才能提出此请求。我的不能在桌面上工作,但可以在网络上工作,但如果你想尝试的话,你可以参考这个链接:https://docs.microsoft.com/en-us/outlook/add-ins/use-rest-api

遇到最低要求问题时。我找到了一种使用 Exchange Web 服务请求 (EWS) 使其工作的方法。它主要使用XML文件发送请求交换Web服务并返回响应。

但是,我发现我需要获取该电子邮件的 MimeContent 才能添加到我的 CreateItem xml 请求中。请检查此链接以获取更多信息。 https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/getitem-operation-email-message

然后,我使用 MimeContent 将现有项目添加到新电子邮件中。您可以使用此链接查看:https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-add-attachments-by-using-ews-in-exchange

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    相关资源
    最近更新 更多