Suresh Konapuram
Suresh Konapuram

Reputation: 13

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);
      });
    }
  });

Upvotes: 1

Views: 1229

Answers (2)

Marc LaFleur
Marc LaFleur

Reputation: 33124

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);
      });
    }
  });

Upvotes: 2

Muthurathinam
Muthurathinam

Reputation: 1042

You are looking for mail body. So, try selecting body instead of bodyPreview.
Here is the graph documentation example which has the body in its response.

Upvotes: 2

Related Questions