1

I'm trying to add an email item as an attachment to a draft email using create-an-item-attachment api, Where I'm providing the Item as the response of get-a-message api, but I'm getting errors like:

@odata.id, @odata.context, @odata.etag
"The annotation 'odata.context' was found. This annotation is either not recognized or not expected at the current position."

& if I manually remove these 2 annotations I get error for these variables.(I stopped after it gave error to Subject)

ReceivedDateTime, SentDateTime, HasAttachments, Subject

The property 'HasAttachments' does not exist on type 'Microsoft.OutlookServices.Item'. Make sure to only use property names that are defined by the type or mark the type as open type.

I did take a look at this SO answer. But I'm not sure if I can use this method to add attachment to the draft email item. I tried calling item.addItemAttachmentAsync() where item being the response of get-a-message api but got error as item.addItemAttachmentAsync is not a function

I feel like I'm doing something wrong here can anyone help.

Edit:

I feel there is some confusion with my question so Let me add more context. My add-in runs on the Inbox email item, so when the user clicks on the add-in, I wish to forward the email Office.context.mailbox.item to some email address as an attachment including the Office.context.mailbox.item attachments and the email headers. This was possible using SOAP api using something like this.

Now that I'm using Rest API, I'm unable to do the exact thing which I have doing using SOAP api, forward the mail item as an attachment with email headers and original email atatchments. I'm creating a new draft item using /createforward and then trying to edit the draft item and attach /attachments.

With the help of this SO post I was able to send the email as an attachment. I ended up removing @odata.context from the message itemAttachment and added "@odata.type" : #Microsoft.OutlookServices.Message to the message itemAttachment. But now the attached email headers are missing in the attachment.

Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
  • With the help of https://stackoverflow.com/a/46954993/3326331 I was able to send the email as an attachment. I ended up removing `@odata.context` from the message itemAttachment and added `"@odata.type" : #Microsoft.OutlookServices.Message` to the message itemAttachment. But now the attached email headers are missing in the attachment. – Sagar Pilkhwal Jan 11 '19 at 07:24
  • item.addItemAttachmentAsync() is an Office.js function meant to be used from within a Outlook Web Add-in. Are you making calls via the add-in api? or via rest? – Outlook Add-ins Team - MSFT Jan 11 '19 at 23:43
  • I'm making rest api calls from within Outlook Web Add-in. – Sagar Pilkhwal Jan 14 '19 at 06:21
  • it appears you have an addin running on a draft e-mail and you want to attach another e-mail to the draft. We don't have an API to obtain this other e-mail. How are you obtaining this other e-mail? – Outlook Add-ins Team - MSFT Jan 22 '19 at 19:14
  • I have edited my question with some more details and progress done so far. – Sagar Pilkhwal Jan 23 '19 at 07:07

1 Answers1

2

To add the item as an attachment you should use the item.addFileAttachmentAsync() function from Office.js.

Here is an example of how to do this:

// Example EWS Item ID
var itemId = "AAMkADU5ODYxOTI2LWQ5ODktNGNkMy05ZmU5LWY4ZWNlMmEwNDI4MwBGAAAAAAC8pAGEht5DRrHaTsDL/q5XBwCys1ms6AKZT7uAgKv13R58ABtsz8d7AABoPf5UVWMrTKxA5Yn7Am3VAAATUR7UAAA=";

Office.context.mailbox.item.addItemAttachmentAsync
(
    itemId,
    "message_name.msg",
    {
        // The values in asyncContext can be accessed in the callback
        "asyncContext" : { foo: 1, bar: 6, baz: true }
    },
    function (asyncResult)
    {
        showMessage(JSON.stringify(asyncResult));
    }
);

Edit: To add current item as attachment to a new draft message you can use the displayNewMessageFormAPI:

Office.context.mailbox.displayNewMessageForm(
{
    htmlBody : "This is a sample with file and item attachments",
    attachments :
    [
        { type: "file", url: "http://i.imgur.com/9S36xvA.jpg", name: "dog.jpg" },
        { type: "item", itemId : Office.context.mailbox.item.itemId, name: "test_email.msg" }
    ],
    options : { asyncContext: null },
    callback : function (asyncResult)
    {
        if (asyncResult.status == "failed")
        {
            showMessage("Action failed with error: " + asyncResult.error.message);
        }
    }
});

You can also add to/cc recipients as per your scenario in the above request.

  • hey bud, sorry but I want to add attachment to a draft item and not the current mailItem from which my add-in is triggered. `Office.context.mailbox.item` will be the inbox mail item in my case unfortunately. – Sagar Pilkhwal Jan 16 '19 at 07:15
  • The functionality you are looking for isn't supported via the Office.js APIs at this time. We track Outlook add-in feature requests on our [UserVoice page](https://officespdev.uservoice.com/forums/224641-general/category/131778-outlook-add-ins). Please request this feature on UserVoice. – Outlook Add-ins Team - MSFT Jan 25 '19 at 18:18
  • 1
    Updated the answer to suggest displayNewMessageForm API as per your updated scenario details. – Outlook Add-ins Team - MSFT Feb 12 '19 at 23:03
  • Isn't there any way to do this operation in the background ? Also once the email is forwarded the attachment is missing original email attachment, if `Office.context.mailbox.item.itemId` contains any attachment it isn't the part of the attached .msg file, the attachment comes next to the the .msg file in the email which is sent. – Sagar Pilkhwal Apr 09 '19 at 06:43
  • We have not been able to reproduce this using the code in the answer, in both desktop Win 32 Outlook and OWA. Which platform are you using and its version? Can you clearly share the repro steps so that we can get back to you. – Outlook Add-ins Team - MSFT May 16 '19 at 06:05
  • @OutlookAdd-insTeam-MSFT I have copied your exact code from above, for displayNewMessageForm and I am unable to create a new window that contains attachments. The window pops up, but the attachments are not there. I have tried opening in the Web version, and Mac Version of Outlook. Should this code still be functioning as you originally intended? I am trying to do something similar to what the original user posted about, which is attach the currently viewed email as an attachment in a new message window, but I can't get any attachments to work following your code or the documentation. – user2023116 Feb 02 '21 at 01:44