无法将附件添加到 Office JS 加载项

Unable to add attachment to Office JS Add-in

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

我首先创建了一个用于将电子邮件保存到草稿的 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>

对我来说看起来很糟糕 XML 例如

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

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

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

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

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

否则,如果您想在阅读模式下操作电子邮件。可以有两种选择。

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

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

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

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