Outlook Web Add-in: Getting unreadable attachments from selected email to a new one

39 Views Asked by At

I want to create a new email with attachments from the selected one and I can create a new email with mailbox.displayNewMessageForm() but when I open the attachment, that I gave the new email, it says the file is unreadable and when I open it in an editor I get this:

{
    "@odata.context": "https://outlook.office.com/api/v2.0/$metadata#Users('')/Messages('')/Attachments/$entity",
    "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
    "@odata.id": "https://outlook.office.com/api/v2.0/Users('')/Messages('')/Attachments('')",
    "@odata.readLink": "https://outlook.office.com/api/v2.0/Users('')/Messages('')",
    "@odata.mediaContentType": "application/pdf",
    "Id": "",
    "LastModifiedDateTime": "2023-11-16T06:45:44Z",
    "Name": "something.pdf",
    "ContentType": "application/pdf",
    "Size": 81012,
    "IsInline": false,
    "ContentId": "",
    "ContentLocation": null,
    "ContentBytes": ""
}

P.S.: I deleted the content in the example

I call a token (otherwie I don't even get the 'json file') from mailbox.getCallbackTockenAsync() and the url of all my attachments from the selected email - Office.context.mailbox.restUrl + '/v2.0/me/messages/' + getEmailId() + '/attachments'.
Then I use ajax to get all the attachments details and I get the exact json from the top. Because the attachments in mailbox.displayNewMessageForm() need a url if they are Office.MailboxEnums.AttachmentType.File needs it.

function CreateNewEmail(attachments) {
      // attachments.values[i] -> contentBytes, ContentId, ContentLocation, ContentType, Id, etc.

      // get attachments from selected email, create a new url and add to array
      let fileArr = [];
      for (let i = 0; i < attachments.value.length; i++) {
        const attachment = attachments.value[i];

        const url = attachmentUrl + '/' + attachment.Id;
        // const url = attachment['@odata.id'];

        fileArr.push({
          itemId: attachment.Id,
          name: attachment.Name,
          inLine: attachment.IsInline,
          type: 'file', 
          url: url
        });
      }

      // new email
      Office.context.mailbox.displayNewMessageForm({
        subject: Office.context.mailbox.item.subject,
        htmlBody: "Helooooooooo my friend!",
        attachments: fileArr
      });
}

I thought when the url was created the outlook would be able to create the attachment, but apperantly not and I can't find out why.
Does anyone know what I'm missing?

0

There are 0 best solutions below