Search code examples
javascriptmicrosoft-graph-apimicrosoft-graph-sdks

How to get the mail body beyond 255 characters?


I am retrieving emails through the following code but I am only getting 255 characters instead of the whole body.

Is there any way to remove this limit?

const api = client
  .api("/me/mailfolders/inbox/messages")
  .top(10)
  .select("subject,from,receivedDateTime,isRead,bodyPreview")
  .orderby("receivedDateTime DESC")
  .get((err, res) => {
    if (err) {
      console.log("getMessages returned an error: " + err.message);
    } else {
      console.log("Mails are retrieving...");

      res.value.forEach(function(message) {
        console.log(message.bodyPreview);
      });
    }
  });

Solution

  • Muthurathinam is correct, but for the sake of clarity and future uses, I'm adding a more in-depth answer.

    Your code is currently requesting only the following properties:

    • subject
    • from
    • receivedDateTime
    • isRead
    • bodyPreview

    The reason why you're only recieving 255 characters of the message is because you're requesting bodyPreview. Looking at the documentation, bodyPreview is defined as follows:

    bodyPreview - String - The first 255 characters of the message body. It is in text format.

    What you're actually looking for is the body property. The body property returns a itemBody object that contains two properties:

    • content - The content of the item.
    • contentType - The type of the content. Possible values are Text and HTML.

    This means that instead of console.log(message.bodyPreview) you will need to use console.log(message.body.content).

    Here is your sample code, refactored to use body:

    const api = client
      .api("/me/mailfolders/inbox/messages")
      .top(10)
      .select("subject,from,receivedDateTime,isRead,body")
      .orderby("receivedDateTime DESC")
      .get((err, res) => {
        if (err) {
          console.log("getMessages returned an error: " + err.message);
        } else {
          console.log("Mails are retrieving...");
    
          res.value.forEach(function(message) {
            console.log(message.body.content);
          });
        }
      });