7

I want to read my email messages and transform them into json. I am using Microsoft Graph API to query the office 365 mail box like this

GraphServiceClient client = new GraphServiceClient(
            new DelegateAuthenticationProvider (
                (requestMessage) =>
                    {
                        requestMessage.Headers.Authorization =
                                            new AuthenticationHeaderValue("Bearer", token);
                        return Task.FromResult(0);
                    }
                )
            );

var mailResults = await client.Me.MailFolders.Inbox.Messages.Request()
                                .OrderBy("receivedDateTime DESC")
                                .Select(m => new { m.Subject, m.ReceivedDateTime, m.From, m.Body})
                                .Top(100)
                                .GetAsync();

I followed this tutorial to get to this stage. But my message body is returned as html instead of text. Is there a way I can specify the message.body to return text or even json instead of HTML?

s-a-n
  • 767
  • 2
  • 9
  • 27

2 Answers2

14

Don't you have to set the HTTP request header:

Prefer: outlook.body-content-type="text"

requestMessage.Headers.Add("Prefer", "outlook.body-content-type='text'");

As per the documentation https://learn.microsoft.com/en-us/previous-versions/office/office-365-api/api/version-2.0/mail-rest-operations

Edit:

View the documentation, this is the client class code: https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/src/Microsoft.Graph/Requests/Generated/GraphServiceClient.cs

Here is an example from the link you're following:

private static GraphServiceClient GetClient(string accessToken, IHttpProvider provider = null)
{
        var delegateAuthProvider = new DelegateAuthenticationProvider((requestMessage) =>
        {
            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);

            return Task.FromResult(0);
        });

        var graphClient = new GraphServiceClient(delegateAuthProvider, provider ?? HttpProvider);

        return graphClient;
 }
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Sure that looks like the option to use. But not sure how to use this in the GraphServiceClient ? Any clue? – s-a-n Jul 24 '18 at 23:22
  • I sure have this code but don't understand where/how do I specify the content-type for the body in this code. – s-a-n Jul 25 '18 at 00:02
  • 2
    You think I should specify something like this? requestMessage.Headers.Add("outlook.body-content-type", "text"); still doesnt work. Any other way to specify the header? – s-a-n Jul 25 '18 at 00:22
  • 1
    Hmm, can you try with the *Prefer:* and a few combinations, eg Prefer as the key and outlook.body-content-type='text' as the value. – Jeremy Thompson Jul 25 '18 at 00:45
  • 3
    Awesome, thank you!!! you a legend mate! requestMessage.Headers.Add("Prefer", "outlook.body-content-type='text'"); worked! – s-a-n Jul 25 '18 at 01:08
  • NB: If your results span multiple pages, meaning you need to do `graphClient.NextPageRequest`, then you need to re-add the header using `graphClient.NextPageRequest.Header("Prefer", "outlook.body-content-type='text'"); graphClient = await graphClient.NextPageRequest.GetAsync();` otherwise page 2 onwards will have HTML content bodies again. – Danny Beckett Aug 21 '20 at 15:14
  • While this option may achieve the goal, it lays down a problem for mocking of the graph client during creation of unit tests. A better solution that supports mocking in unit testing is here: https://stackoverflow.com/questions/54183526/specifying-request-headers-using-c-sharp-office-graph-sdk-client – Sn3akyP3t3 Jul 22 '21 at 06:17
3

With the GraphServiceClient, call .Header("Prefer", "outlook.body-content-type='text'") as part of your request method chain:

var mailResults = await client.Me.MailFolders.Inbox.Messages
                                .Request()
                                .Header("Prefer", "outlook.body-content-type='text'")
                                .OrderBy("receivedDateTime DESC")
                                .Select(m => new { m.Subject, m.ReceivedDateTime, m.From, m.Body})
                                .Top(100)
                                .GetAsync();
micycle
  • 3,328
  • 14
  • 33