网页插件展望

Web Addin Outlook

我正在使用 EWS 和 JavaScript 为 Outlook 开发一个新的网络插件。范围是选择当前电子邮件并将其作为附件添加到新电子邮件中。按照此处的说明进行操作:https://msdn.microsoft.com/en-us/library/office/dn726694(v=exchg.150).aspx#bk_createattachews -> 我创建了一些函数来执行 MSDN 文档中解释的那些步骤。问题是我没有得到任何错误或可以告诉我我做错了什么的东西。这是我的代码:

 function soapToForwardItemCallback(asyncResult) {
        var parser;
        var xmlDoc;

        if (asyncResult.error != null) {
            app.showNotification("EWS Status", asyncResult.error.message);
        }
        else {
            var response = asyncResult.value;
            if (window.DOMParser) {
                parser = new DOMParser();
                xmlDoc = parser.parseFromString(response, "text/xml");
            }
            else // Older Versions of Internet Explorer
            {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = false;
                xmlDoc.loadXML(response);
            }

            // Get the required response, and if it's NoError then all has succeeded, so tell the user.
            // Otherwise, tell them what the problem was. (E.G. Recipient email addresses might have been
            // entered incorrectly --- try it and see for yourself what happens!!)
            var result = xmlDoc.getElementsByTagName("m:ResponseCode")[0].textContent;
            if (result == "NoError") {
                app.showNotification("EWS Status", "Success!");
            }
            else {
                app.showNotification("EWS Status", "The following error code was recieved: " + result);
            }
        }
    }







    function getCurrentEmail() {


             var item = Office.context.mailbox.item;
             item_id = item.itemId;

            var getMimeContent = '<?xml version="1.0" encoding="utf-8"?>' +
                                  '<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>' +
                            //  '    <RequestServerVersion Version="Exchange2013" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" soap:mustUnderstand="0" />' +
                           //  '  </soap:Header>' +
                                  '<soap:Body>' +
                                    '<m:GetItem>' +
                                //     ' xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"' +
                                //  ' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
                                        '<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="' + item_id + '"/>' +
                                               '</m:ItemIds>' +
                                           '</m:GetItem>' +
                                      '</soap:Body>' +
                                   '</soap:Envelope>'

            mailbox.makeEwsRequestAsync(getMimeContent, createMail);


    }

   function createMail(asyncResult) {
                var parser = new DOMParser();
                var xmlDoc;

                if (asyncResult.error !== null) {
                    app.showNotification("EWS Status", asyncResult.error.message);
                }
                else {
                    var response = asyncResult.value;
                    if (window.DOMParser) {

                        xmlDoc = parser.parseFromString(response, "text/xml");
                    }
                    else // Older Versions of Internet Explorer
                    {
                        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                        xmlDoc.async = false;
                        xmlDoc.loadXML(response);
                    }



                    var toAddresses = 'alexandru.banica@rodacsoft.ro';

                    var addressesSoap = "";

                    addressesSoap += "<t:Mailbox><t:EmailAddress>" + toAddresses + "</t:EmailAddress></t:Mailbox>";

                    var soapToCreateNewEmail = '<?xml version="1.0" encoding="utf-8"?>'+
                                               '<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="Exchange2013" />'+
                                               '     </soap:Header>'+
                                               '     <soap:Body>'+
                                               '     <m:CreateItem MessageDisposition="SaveOnly">'+
                                               '     <m:Items>'+
                                               '     <t:Message>'+
                                               '     <t:Subject>Message with Item Attachment (MimeContent)</t:Subject>'+
                                               '     <t:Body BodyType="HTML">The attachmen</t:Body>'+
                                               '     <t:ToRecipients>'+ addressesSoap
                                               +
                                               ' </t:ToRecipients>'+
                                               ' </t:Message>'+
                                               ' </m:Items>'+
                                               ' </m:CreateItem>'+
                                               ' </soap:Body>'+
                                               ' </soap:Envelope>'

                    mailbox.makeEwsRequestAsync(soapToCreateNewEmail,soapToGetItemDataCallback);

                }
 }


 function createAttachement(asyncResult) {

     var parser = new DOMParser();
     var xmlDoc;

     if (asyncResult.error !== null) {
         app.showNotification("EWS Status", asyncResult.error.message);
     }
     else {
         var response = asyncResult.value;
         if (window.DOMParser) {

             xmlDoc = parser.parseFromString(response, "text/xml");
         }
         else // Older Versions of Internet Explorer
         {
             xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
             xmlDoc.async = false;
             xmlDoc.loadXML(response);
         }

         var mimeTag = xmlDoc.getElementsByTagName("t:MimeContent")[0].innerText;

         var soapToCreateAttachement = '<?xml version="1.0" encoding="utf-8"?>'+
                                        ' <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="Exchange2013" />'+
                                                     '        </soap:Header>'+
                                                      '           <soap:Body>'+
                                                         '             <m:CreateAttachment>'+
                                                         '             <m:ParentItemId Id="'+item_id+'" />'+
                                                         '             <m:Attachments>'+
                                                         '             <t:ItemAttachment>'+
                                                          '            <t:Name>Play tennis?</t:Name>'+
                                                         '             <t:IsInline>false</t:IsInline>'+
                                                          '           <t:Message>'+
                                                         '           <t:MimeContent CharacterSet="UTF-8">'+ mimeTag +'</t:MimeContent>'+
                                                         '            </t:Message>'+
                                                         '        </t:ItemAttachment>'+
                                                         '       </m:Attachments>'+
                                                         '      </m:CreateAttachment>'+
                                                         '     </soap:Body>'+
                                                         '    </soap:Envelope>'


         mailbox.makeEwsRequestAsync(soapToCreateAttachement,sendEmailAsAttachement);
     }
 }

 function sendEmailAsAttachement(asyncResult) {

     var parser = new DOMParser();
     var xmlDoc;

     if (asyncResult.error !== null) {
         app.showNotification("EWS Status", asyncResult.error.message);
     }
     else {
         var response = asyncResult.value;
         if (window.DOMParser) {

             xmlDoc = parser.parseFromString(response, "text/xml");
         }
         else // Older Versions of Internet Explorer
         {
             xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
             xmlDoc.async = false;
             xmlDoc.loadXML(response);
         }

         var changeKey = xmlDoc.getElementsByTagName("t:ItemId")[0].getAttribute("ChangeKey");

         var soapToSendEmailAttachment = ' <?xml version="1.0" encoding="utf-8"?>' +
                                        ' <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="Exchange2013" />' +
                                        '            </soap:Header>' +
                                        '            <soap:Body>' +
                                        '            <m:SendItem SaveItemToFolder="true">' +
                                        '            <m:ItemIds>' +
                                        '                <t:ItemId Id="'+ item_id +'"' +
                                        '                    ChangeKey="'+ changeKey +'" />' +
                                        '                    </m:ItemIds>' +
                                        '                <m:SavedItemFolderId>' +
                                        '             <t:DistinguishedFolderId Id="sentitems" />' +
                                        '        </m:SavedItemFolderId>' +
                                        '    </m:SendItem>' +
                                        '  </soap:Body>' +
                                        '  </soap:Envelope>'

         mailbox.makeEwsRequestAsync(soapToSendEmailAttachment, soapToForwardItemCallback);
     }
 }

单击按钮时调用的第一个函数是 getCurrentEmail()。我不确定我是否可以那样进行 SOAP 调用。任何帮助将不胜感激!如果您需要更多信息,请告诉我。谢谢!

I am not sure if I can do the SOAP calls like that.

以下资源描述了 EWS operations that add-ins support。 "m:CreateAttachment" 不是其中之一;也许你应该从那里开始。

编辑:

我不明白你怎么没有收到任何错误。你在调试吗?你能打断点吗?好吧,这一切都很奇怪。 现在让我告诉你我做了什么以及我注意到你的代码有什么问题......

  1. 我创建了简单项目并添加了您的代码。我调用了 "getCurrentEmail()" 函数并仅使用该函数。我没有验证任何其他功能,因为我相信你应该自己做。

  2. Javascript 错误:如果你使用 "strict" JS 模式(你应该!)函数有 "item_id" 变量,它没有定义为 "var".您应该使用 "var" 关键字将其设为本地。接下来,当您调用 "mailbox.makeEwsRequestAsync" 时,"mailbox" 变量也是未定义的。你应该称它为 "Office.context.mailbox.makeEwsRequestAsync"。

  3. EWS 请求的格式根本不正确。我不知道,也许你玩过它,但是你提供的资源中的示例 EWS XML 与你通过插入消息 ID 创建的不同。当我修复 JS 错误(查看上面的第 2 点)并发送 EWS 请求时,它返回开始 "Error" 并正确描述请求格式错误。我已更改请求以匹配示例中提供的请求,它 returns "success" 和您请求的信息,如主题和 MIME 内容。

我将仅针对您要求的功能分享我的代码,其余的请自行尝试。给你...

      function _wrapSoapEnvelope(payload) {
            var result = '<?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:m="http://schemas.microsoft.com/exchange/services/2006/messages" ' +
                'xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
            '<soap:Header>' +
            '<t:RequestServerVersion Version="Exchange2013" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" soap:mustUnderstand="0" />' +
            '</soap:Header>' +
            '<soap:Body>' + payload + '</soap:Body>' +
            '</soap:Envelope>';
            return result;
        };

        function getCurrentEmail() {
            var item = Office.context.mailbox.item,
            item_id = item.itemId;
            var getMimeContent = '<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="' + item_id + '"/>' +
                                  '</m:ItemIds>' +
                                  '</m:GetItem>'

            Office.context.mailbox.makeEwsRequestAsync(_wrapSoapEnvelope(getMimeContent), createMail);
        }