locked
Office 365 Mail Rest Api is not Returning "ContentBytes" property of an email attachment. RRS feed

  • Question

  • How to get ContentBytes property of an email attachment using office 365 rest api? I am using node.jsbelow is the code which i am using


    outlookClient.me.folders.getFolder('Inbox').messages.getMessages().filter('IsRead eq false and HasAttachments eq true').fetchAll(10).then(function (result) {
    result.forEach(function (message) {
        message.attachments.getAttachments().fetchAll(100).then(function (attachmentResult) {
            console.log('Message "' + message.subject + '" contains ' + attachmentResult.length + ' attachment(s)');
            attachmentResult.forEach(function (attachment) {
              console.log('*    "' + attachment.name + '" (' + attachment.size + ' KB)');
            });
        });
    });

    });

    in above code i am able to get all the details of attachment except ContentBytes,

    I wan to get all attachment type(item attachment and file attachment) content bytes from an email.

    please suggest me how can i get the ContentBytes of an attachment.


    • Edited by rvkp Sunday, August 9, 2015 3:52 PM
    Sunday, August 9, 2015 3:52 PM

Answers

  • Hi rvkp,

    >> Office 365 Mail Rest Api is not Returning "ContentBytes" property of an email attachment

    Based on the API below, FileAttachment contains ContentBytes, but ItemAttachment did not contain ContentBytes.

    # Resource reference for the Mail, Calendar, and Contacts REST APIs
    https://msdn.microsoft.com/office/office365/APi/complex-types-for-mail-contacts-calendar#FileAttachmentResource

    With getAttachments, it return the ItemAttachment list instead of FileAttachment. With C#, you could convert the ItemAttachment to FileAttachment. Something like below:

                var outlookServicesClient = await AuthenticationHelper.EnsureOutlookServicesClientCreatedAsync("Mail");
                var messages = await outlookServicesClient.Me.Folders["Inbox"].Messages
                    .Where(m => m.HasAttachments == true)
                    .Expand(m => m.Attachments)
                    .Take(10)
                    .ExecuteAsync();
                foreach (var message in messages.CurrentPage)
                {
                    var attachments = message.Attachments.CurrentPage;
                    foreach (FileAttachment attachment in attachments)
                    {
                        byte[] test = attachment.ContentBytes;
                    }
                }

    But I am not sure whether this way could be achieved in javascript. If this could not be achieved, I think you need to get the specific attachment with message_id and attachment_id and use http request to get attachment. In the response of the Get an attachment (REST), it will return ContentBytes.

    # Get an attachment (REST)
    https://msdn.microsoft.com/en-us/office/office365/api/mail-rest-operations#Getattachments

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, August 10, 2015 1:46 PM