6

我想阅读我的电子邮件并将它们转换为 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();

我按照教程进入了这个阶段。但是我的消息正文以 html 而不是文本形式返回。有没有办法可以指定 message.body 返回文本甚至 json 而不是 HTML?

4

2 回答 2

10

您不必设置 HTTP 请求标头:

首选:outlook.body-content-type="text"

requestMessage.Headers.Add("Prefer", "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;
 }
于 2018-07-24T07:12:34.520 回答
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();
于 2020-12-02T17:53:00.827 回答