2
votes

I've been successfully getting the list of mails in inbox using microsoft graph rest api but i'm having tough time to understand documentation on how to download attachments from mail.

enter image description here

For example : This question stackoverflow answer speaks about what i intend to achieve but i don't understand what is message_id in the endpoint mentioned : https://outlook.office.com/api/v2.0/me/messages/{message_id}/attachments

UPDATE

i was able to get the details of attachment using following endpoint : https://graph.microsoft.com/v1.0/me/messages/{id}/attachments and got the following response.

enter image description here

I was under an impression that response would probably contain link to download the attachment, however the response contains key called contentBytes which i guess is the encrypted content of file.

2
Is the value of the body property the data that you are trying to download?guest271314
There is a PDF attachment in the mail. i want to download that. HasAttachment flag is also true in the resulting jsonIrfan Harun
Have not tried MS-grapgh. What property value of the object is a PDF file?guest271314
@guest271314 : I was able to get the details of the attachment using api endpoint : https://graph.microsoft.com/v1.0/me/messages/{id}/attachmentsIrfan Harun

2 Answers

1
votes

For attachment resource of file type contentBytes property returns

base64-encoded contents of the file

Example

The following Node.js example demonstrates how to get attachment properties along with attachment content (there is a dependency to request library):

const attachment = await getAttachment(
    userId,
    mesasageId,
    attachmentId,
    accessToken
);
const fileContent = new Buffer(attachment.contentBytes, 'base64');
//...

where

const requestAsync = options => {
  return new Promise((resolve, reject) => {
    request(options, (error, res, body) => {
      if (!error && res.statusCode == 200) {
        resolve(body);
      } else {
        reject(error);
      }
    });
  });
};

const getAttachment = (userId, messageId, attachmentId, accessToken) => {
  return requestAsync({
    url: `https://graph.microsoft.com/v1.0/users/${userId}/messages/${messageId}/attachments/${attachmentId}`,
    method: "GET",
    headers: {
      Authorization: `Bearer ${accessToken}`,
      Accept: "application/json;odata.metadata=none"
    }
  }).then(data => {
    return JSON.parse(data);
  });
};

Update

The following example demonstrates how to download attachment as a file in a browser

try {
  const attachment = await getAttachment(
    userId,
    mesasageId,
    attachmentId,
    accessToken
  );

  download("data:application/pdf;base64," +  attachment.contentBytes, "Sample.pdf","application/pdf");
} catch (ex) {
  console.log(ex);
}

where

async function getAttachment(userId, messageId, attachmentId, accessToken){
    const res = await fetch(
      `https://graph.microsoft.com/v1.0/users/${userId}/messages/${messageId}/attachments/${attachmentId}`,
      {
        method: "GET",
        headers: {
          Authorization: `Bearer ${accessToken}`,
          Accept: "application/json;odata.metadata=none"
        }
      }
    );
    return res.json();
 }

Dependency: download.js library

0
votes

I don't know if this would help but you just have to add /$value at the end of your request :

https://graph.microsoft.com/v1.0/me/messages/{message_id}/attachments/{attachment_id}/$value