0

I try to find solution to my problems but didn't find any where,hope that someone here can save me.

I write add-in in JavaScript on VS2015 that encrypte and decrypte body messages.

1. The first problem is with images that the receiver can't see . (Talk about images that copy into the body by "insert picture inline" button)

In Compose mode we encrypte the message and then when we decrypte it's works good because the compose mode is a client side and he his recognize the local images . In read mode when user want to decrypte the message and to see the images he couldn't see because the encrypte prevent outlook to convert the local image to data on the server .

In my code I take the body message like this ( compose mode )

item.body.getAsync(
                       item.body.getAsync(
                                    "html",
                                  { asyncContext: "This is passed to the callback" },
                                   function callback(resultbody) { 

                                    ......Here we send the body for ENCRYPT.

                }))

then , the user send the encrypte message by clicking 'send' regular.

In the read mode I just print it to my html to check if the decrypte is good :

(JSON.parse(xhr.responseText).Data.Content));

and then i get icon of picture ,but not success to show the real pic . The src of the icon is going for place that not access for him ..

<img src="https://attachment.outlook.office.net/owa/*****/service.svc/s/GetFileAttachment?id=AAMkADUwMDE0YWM1LTYwODctNG ......

How can i take this tag of image and do something that the receiver can see the image ? I don't want that user will be need to upload image to body from my my add-in instead of the original outlook. I try to convert the image to base-64 string, but with what I have in the tag it not enough ,just with original picture and also it success to show in html but not in the body of message with SetAsync function..

2. The second problem is with attachments . I upload files with dropzone plug-in (because outlook don't give access to take attachment and change him). So, after I upload files and encrypte him I make some new file with the response from server with File API of JS :

ar f = new File([""], "filename.txt", {type: "text/plain", lastModified: date}) . .. .

than I want to attach the file to mail, so the only method that do this is:

addFileAttachmentAsync(uri, attachmentName, optionsopt, callback opt)

then,I need to create a url for file for this method so I use this method:

var objectURL = URL.createObjectURL(f);

But now when I use the method addFileAttachmentAsync with objectURL it's write that there is a problem and its can't attach it , I think that the URL is incorrect .

Thanks all!!

4

1 回答 1

5

对于希望解决此问题的每个人..

**在 Outlook Web 中,此解决方案运行良好,但在 Outlook Desktop 中存在与服务器同步的问题,因此 saveAsync 功能存在延迟,目前没有任何解决方案,所以它可以工作,但需要稍等一下。你可以在这里阅读更多信息。

第一个问题:

在使用 getAsync 和 setAsync 函数时,Outlook 加载项中存在问题。当体内有一些图像时,就会出现问题。发生这种情况是因为当您以 Html 格式获取正文然后返回具有一些不同的正文时,图像仍然没有“上传”并且 src 是错误的。我使用 Outlook rest API 成功解决了这个问题。所以解决方法是这样的:

  1. 通过 getAsync 方法获取 Html 类型的正文消息。创建 div 元素并在 div 内设置返回正文消息。
  2. 要获取消息 ID,您需要使用 saveAsync 函数将消息保存为草稿。
  3. 要向 Outlook REST API 发出请求,您需要获取访问令牌,因此调用 getCallbackTokenAsync 函数并保存访问令牌。
  4. 向 Outlook REST API 发出 Http 请求以获取消息中存在的所有附件。
  5. 找到您的图像的正确 ID,并将图像 src 替换为您从对 Outlook REST API 的请求中获得的图像的 base-64。
  6. 最后,您可以使用 SetAsync 函数设置您的新主体。

代码:

item.body.getAsync(
Office.CoercionType.Html,
{ asyncContext: "This is passed to the callback" },
function callback(resultbody) {
  var bodyDiv = document.createElement('div');
  bodyDiv.innerHTML = content;
  Office.context.mailbox.item.saveAsync(
  function callback(result) {
    var myNewItemSaved = result.value;                                                                                            
    Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, 
     function (result) {
       if (result.status === "succeeded") {  
          var accessToken = result.value;
          var itemId = ""; 
          if (Office.context.mailbox.diagnostics.hostName === 'OutlookIOS')
             itemId = Office.context.mailbox.item.itemId; 
          else    
            itemId = Office.context.mailbox.convertToRestId(myNewItemSaved,
                     Office.MailboxEnums.RestVersion.v2_0); 

      var xhr3 = new XMLHttpRequest();
      xhr3.open("GET", "https://outlook.office.com/api/v2.0/me/messages/" +    itemId + "/attachments", true);
      xhr3.setRequestHeader("Content-type", "application/json"); 
      xhr3.setRequestHeader("Access-Control-Allow-Origin", "*");  
      xhr3.setRequestHeader("Authorization", "Bearer " + accessToken); 
      xhr3.send();
      xhr3.onreadystatechange = function () {  
      if (xhr3.readyState == 4) {
        if (xhr3.status == 200) {  
           var allImages = JSON.parse(xhr3.response).value;
           var isDesktop = false;
           var imgSrcId = bodyDiv.getElementsByTagName('img')[0].getAttribute("src");
           if (imgSrcId.indexOf("cid") != -1) //Outlook Desktop
                  isDesktop = true;
          for (var i = 0; i < allImages.length; i++) {
              if (bodyDiv.getElementsByTagName('img')[i].getAttribute("src").indexOf("base64")!=-1)
                   continue;
            if (isDesktop)
             imgSrcId = bodyDiv.getElementsByTagName('img')[i].getAttribute("src");
             else
               imgSrcId =  bodyDiv.getElementsByTagName('img'[i].getAttribute("originalsrc");                                                                                                                   

           imgSrcId = imgSrcId.substr(4, imgSrcId.length);

          var wantedImg;
          for (var j = 0; j < allImages.length; j++) {
            if ((allImages[j].ContentId).localeCompare(imgSrcId) != -1) {
                           wantedImg = allImages[j]; break;}
           }
       bodyDiv.getElementsByTagName('img')[i].src = 'data:' + wantedImg.ContentType + ';base64,' + wantedImg.ContentBytes;
        }
    }
   setAsync......

  }
  }}}})})};

第二个问题

addFileAttachmentAsync 的问题是,这仅适用于外部服务器上的文件,而不是添加 blob 本地文件。所以这里的解决方案也是 Outlook rest API 。该解决方案会将我们的文件附加到消息中,但我们看不到这个 - 在 message 中没有附件的预览,但是当我们发送它时,它将附加到 message ,我们可以在我们的消息中看到附件在那里。该解决方案与正文中的图像非常相似 - 将消息另存为草稿,获取访问令牌,这次 Http 请求将是对我们的消息 ID 的“POST”请求,以将我们的文件附加到当前消息。

向消息添加附件的请求的代码(一直到这里与问题 1 相同):

var attachment ={
     "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
      "Name": "smile.png",
      "ContentBytes": "AAACFAMxLjAeKUDndY7EKF4P7QiWE7HgHLa7UiropGUTiDp5V07M0c5jaaTteauhzs0hOU+EOmVT0Lb6eSQ2MzgkCre/zCV9+kIB9PjWnOzoufau67J9PQdXapsOQSMcpt9X2QpcIjnl7H3sLu9iu2rqcvSjwhDnK6JygtghUB405EZHZ9LQcfJ1ZTYHylke2T9zbViq2BPqU/8IHZWsb/KQ/qzV4Jwv3NHnI583JvOuAtETJngh964edC4cU2IY6FkIWprksRw7d4fEQ/+3KbEyW0trIZm59jpTSV01/PhOI0RDKj1xI1Vr+lgMRZpOrYDfChWWWbByNzSXbIsTjHMU6GmQ5Cb09H3kv/2koFa5Pj2z8i+NGywYKw8ZSu3NVblM9I0EkQVLrxkM8gqyrDEtAobxPRxEzGTEXdnjws5UIiiGFBq3khuxejFGCNvUbmPM9guVZO0ccDe1FICTFHkrPlLZW/TvJYMou0HBrvH7s4taBHyZw5x03dhps+WG19D5na44vaVX2Vni6ZrrxfqFo7JTUpCJxCcPyoG7/nEWtJ/V/J+oXdypeapN9Agl6Q81WvCbzuyZgbLTfj6NXWDoliie069Hvk/k2lP+HyO7Iu5ffeRX2WWguwdfGXiNbqInrxn18tX+N7/KqWbRJv96tmijdCmCvsF9Lpr9k7QFKB93wuHfTuE6Qi2IVNBfzNBaz1iJYjY="
    } 
    var xhr4 = new XMLHttpRequest();             
    xhr4.open("POST", "https://outlook.office.com/api/v2.0/me/messages/" + itemId + "/attachments", true); 
    xhr4.setRequestHeader("Content-type", "application/json");
    xhr4.setRequestHeader("Access-Control-Allow-Origin", "*"); 
    xhr4.setRequestHeader("Authorization", "Bearer " + accessToken);
    xhr4.send(JSON.stringify(attachment));
    xhr4.onreadystatechange = function () {                                                                                                    
                   if (xhr4.readyState == 4) { 
                     if (xhr4.status == 200)
                           console.log("ok");
                     else
                          console.log(xhr4.response);
                    }};                                                             

希望它会帮助某人,祝你好运!

于 2017-03-02T15:28:47.573 回答