soap - 使用 EWS 发送带有另一封邮件作为附件的新邮件

标签 soap office365 exchangewebservices office-js outlook-web-addins

我正在开发“outlook-web-addon”,它将当前打开的消息附加到新消息并将其发送到特定地址..所有这些都不允许用户交互(无撰写屏幕)

下面的代码被传递到“创建附件”,这会生成错误: “请求的 Web 方法对该调用者或应用程序不可用”

function sendMessage() {
    //$("#y").load("x.html");
    //var mailBody = document.load("MailBody");
    //<object type="text/html" data="mailBody.html"></object>

    if (Office.context.mailbox.item.itemType !== Office.MailboxEnums.ItemType.Message) {
        return;
    }

    var parentId='';
    var mimeContent='';

    //Get current selected mail message Id
    var mailbox = Office.context.mailbox;
    var item = mailbox.item;
    var itemId = item.itemId;


    //1- Create Mime content from the current selected mail message
    var request_MimeContent =
        '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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:Body>' +
            '       <m:GetItem>' +
            '           <m:ItemShape>' +
            '               <t:BaseShape>IdOnly</t:BaseShape>' +
            '               <t:AdditionalProperties>' +
            '                   <t:FieldURI FieldURI="item:MimeContent" />' +
            '                   <t:FieldURI FieldURI="item:Subject" />' +
            '               </t:AdditionalProperties>' +
            '           </m:ItemShape>' +
            '           <m:ItemIds>' +
            '               <t:ItemId Id="' +itemId +'" />' +
            '           </m:ItemIds>' +
            '       </m:GetItem>' +
            '    </soap:Body>' +
            '</soap:Envelope>';
    Office.context.mailbox.makeEwsRequestAsync(request_MimeContent, createMail);
}

function createMail(asyncResult) {
    if (asyncResult.status == "failed") {
        return;
    } else {
        //2- Get MimeContent
        var response = $.parseXML(asyncResult.value);
        window.mimeContent = response.getElementsByTagName("MimeContent");

        //3- Create mail request
        var request_CreateMail =
            '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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><t:RequestServerVersion Version="Exchange2010" /></soap:Header>' +
                '  <soap:Body>' +
                '    <m:CreateItem MessageDisposition="SaveOnly">' +
                '      <m:SavedItemFolderId><t:DistinguishedFolderId Id="sentitems" /></m:SavedItemFolderId>' +
                '      <m:Items>' +
                '        <t:Message>' +
                '          <t:Subject>Phishing Mail Report!</t:Subject>' +
                '          <t:Body BodyType="HTML">This is a Test !!</t:Body>' +
                '          <t:ToRecipients>' +
                '            <t:Mailbox><t:EmailAddress><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3ee8df9cac2c7c2e3f9cacdc2c78dcdc6d7" rel="noreferrer noopener nofollow">[email protected]</a></t:EmailAddress></t:Mailbox>' +
                '          </t:ToRecipients>' +
                '        </t:Message>' +
                '      </m:Items>' +
                '    </m:CreateItem>' +
                '  </soap:Body>' +
                '</soap:Envelope>';
        Office.context.mailbox.makeEwsRequestAsync(request_CreateMail, createAttachment);
    }
}

function createAttachment(asyncResult) {
    if (asyncResult.status == "failed") {
        return;
    } else {
        //4- Get new message Id
        var response = $.parseXML(asyncResult.value);
        window.parentId = response.getElementsByTagName("ItemId");

        //5- Create attachment from the mime content and belongs to the new message
        var request_CreateAttachment =
            '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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><t:RequestServerVersion Version="Exchange2010" /></soap:Header>' +
                '    <soap:Body>' +
                '       <m:CreateAttachment>' +
                '           <m:ParentItemId Id="' +window.parentId +'" />' +
                '           <m:Attachments>' +
                '               <t:ItemAttachment>' +
                '                   <t:Name>Play tennis?</t:Name>' +
                '                   <t:IsInline>false</t:IsInline>' +
                '                   <t:Message>' +
                '                       <t:MimeContent CharacterSet="UTF-8">' +window.mimeContent +'</t:MimeContent>' +
                '                   </t:Message>' +
                '               </t:ItemAttachment>' +
                '           </m:Attachments>' +
                '       </m:CreateAttachment>' +
                '    </soap:Body>' +
                '</soap:Envelope>';
        Office.context.mailbox.makeEwsRequestAsync(request_CreateAttachment, sendItem);
    }
}

function sendItem(asyncResult) {
    if (asyncResult.status == "failed") {
        return;
    } else {
        //6- Get attachment Id
        var response = $.parseXML(asyncResult.value);
        var attachmentId = response.getElementsByTagName("AttachmentId");

        //7- Send newly created message
        var request_SendItem =
            '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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><t: RequestServerVersion Version="Exchange2010" /></soap:Header>' +
                '    <soap:Body>' +
                '       <m:SendItem SaveItemToFolder="true">' +
                '           <m:ItemIds>' +
                '               <t:ItemId Id="' +window.parentId +'"/>' +
                '           </m:ItemIds>' +
                '           <m:SavedItemFolderId>' +
                '               <t:DistinguishedFolderId Id="sentitems" />' +
                '           </m:SavedItemFolderId>' +
                '       </m:SendItem>' +
                '    </soap:Body>' +
                '</soap:Envelope>';
        Office.context.mailbox.makeEwsRequestAsync(request_SendItem,
            function(asyncResult2) {
                if (asyncResult2.status == "failed") {
                    return;
                } else {
                }
            });
    }
}

我正在尝试使用 Chrome,但出现以下错误: “请求的 Web 方法对该调用者或应用程序不可用”

另外,我想知道这是否适用于桌面 Outlook 应用程序?

最佳答案

根据https://learn.microsoft.com/en-us/outlook/add-ins/understanding-outlook-add-in-permissions#readwritemailbox-permission MakeEWSRequestAsync 不允许执行附件操作。您可以使用 ReadOnly 方法来访问附件 https://learn.microsoft.com/en-us/outlook/add-ins/get-attachments-of-an-outlook-item (但我不相信这会让您创建一个)。

我知道的唯一解决方法是,因为允许 CreateItem,并且如果您要发送的消息首先在 Mime 中构建(对附件 mime 进行编码),那么您可以从 MIMEContent 创建消息,然后从中创建消息该内容应该有效。

关于soap - 使用 EWS 发送带有另一封邮件作为附件的新邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57237275/

相关文章:

c# - EWS - 超时后续订?

html - EWS Managed API、Appointment MessageBody 以及 HTML/CSS 的销毁

logging - 使用 xml pretty-print 的 SOAP 跟踪日志查看器

php - fatal error : Class 'SoapClient' not found

oauth - microsoft_graph oAuth 刷新错误

azure - 在组织中的 Office 365 上发布 Office 加载项

php - 如何排除 PHP-EWS/SOAP 错误?

perl - 如何在 Perl 中创建 SOAP 服务器?

java - 如何在soaphandler中访问@PersistenceUnit私有(private)EntityManagerFactory emf

office365 - AADSTS90093 : Does not have access to consent in office365