Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Graph API returns mail message body as HTML

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?

like image 745
s-a-n Avatar asked Jul 24 '18 06:07

s-a-n


1 Answers

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://docs.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;
 }
like image 160
Jeremy Thompson Avatar answered Nov 15 '22 10:11

Jeremy Thompson