Microsoft Graph API将邮件正文作为HTML返回

时间:2018-07-24 06:24:13

标签: c# office365 microsoft-graph office365api microsoft-graph-security

我想阅读我的电子邮件并将其转换为json。我正在使用Microsoft Graph API这样查询Office 365邮箱

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

我按照this教程进行了学习。但是我的邮件正文以html而不是文本形式返回。有没有一种方法可以指定message.body返回文本甚至json而不是HTML?

2 个答案:

答案 0 :(得分:4)

您不必设置HTTP请求标头:

  

首选:outlook.body-content-type =“ text”

根据文档https://docs.microsoft.com/en-us/previous-versions/office/office-365-api/api/version-2.0/mail-rest-operations

编辑:

查看文档,这是客户端类代码:https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/src/Microsoft.Graph/Requests/Generated/GraphServiceClient.cs

以下是您关注的链接中的一个示例:

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

答案 1 :(得分:0)

使用GraphServiceClient,在您的请求方法链中调用.Header("Prefer", "outlook.body-content-type='text'")

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();
相关问题