使用 MS Graph API C# 读取用户电子邮件 [英] Reading user emails using MS Graph API C#

查看:84
本文介绍了使用 MS Graph API C# 读取用户电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 MS Graph API 从特定邮箱读取电子邮件.

I'm trying to use MS Graph API to read emails from a specific mailbox.

var client = await GetClient(); //getting a client with client id, secret
var users = await client.Users.Request()
                 .Filter("startswith(displayName,'roger')")
                 .GetAsync(); //getting the users matching a criteria

var user = users.First(); //get the first user

//log the user name, this works fine
log.LogInformation("Found user " +  user.DisplayName);

//this is null
var messages = user.MailFolders?.FirstOrDefault();

我从这里获取的用户那里获得了所有正确的数据,但用户 mailFolders 属性为 null.

I get all the correct data from the user I fetch here but the user mailFolders property is null.

这是为什么?

我们希望扫描特定邮箱中的电子邮件并处理这些电子邮件和附件.我认为这可能是做到这一点的正确方法.但我坚持上述内容,MS Graph 的文档,尤其是 .NET API 的文档是如此.

We want to scan for emails in a specific mailbox and process those emails and attachments. and I thought this could be a proper way to do this. But I'm stuck on the above and the documentation on MS Graph and especially the .NET API's are so so.

是不是权限的事情,能不能提高一下我们AD申请注册的权限来获得这个权限?

Is it an authority thing, can I somehow increase the privilege of our AD application registration to get this privilege?

或者这里还有其他事情吗?

Or is there something else going on here?

推荐答案

Graph Client 只是 REST API 的包装器,它不会延迟加载对象.

The Graph Client is just a wrapper around the REST API and it doesn't do lazy loading of objects.

User 对象来自 AAD,而 MailFolders 来自 Exchange,每个对象都是自己的端点.

The User object is coming from AAD while MailFolders comes from Exchange and each as it's own endpoints.

正如您所指出的,您的 Users 请求工作正常.为了检索用户的 MailFolders,您需要从 User 中获取 IdUserPrincipalName 并将其用于对 MailFolders 发出单独的请求.您还需要发出另一个请求来获取消息:

As you noted, you're Users request is working properly. In order to retrieve the user's MailFolders you need to take the Id or UserPrincipalName from the User and use it to make a sperate request for the MailFolders. You'll also need to make another request to get the messages:

// Get all users with a displayName of roger*
var users = await graphClient
    .Users
    .Request()
    .Filter("startswith(displayName,'roger')")
    .GetAsync();

// Get the first user's mail folders
var mailFolders = await graphClient
    .Users[users[0].Id] // first user's id
    .MailFolders
    .Request()
    .GetAsync();

// Get messages from the first mail folder
var messages = await graphClient
    .Users[users[0].Id] // first user'd id
    .MailFolders[mailFolders[0].Id] // first mail folder's id
    .Messages
    .Request()
    .GetAsync();

如果您只关心众所周知"的邮件文件夹,您可以使用 知名名称.例如,您可以像这样请求 inbox:

If you only care about the "well-known" mail folders, you can simplify this request by using the well-known name. For example, you can request the inbox like this:

// Get message from the user's inbox
var inboxMessages = await graphClient
    .Users[users[0].Id] // first user'd id
    .MailFolders["inbox"]
    .Messages
    .Request()
    .GetAsync();

鉴于您仅使用 id 值,您可以通过仅请求 id 属性来优化此:

Given that you're only using id values, you can optimize this by requesting only the id property:

// Get all users with a displayName of roger*
var users = await graphClient
    .Users
    .Request()
    .Select("id") // only get the id
    .Filter("startswith(displayName,'roger')")
    .GetAsync();

// Get the first user's mail folders
var mailFolders = await graphClient
    .Users[users[0].Id] // first user's id
    .MailFolders
    .Request()
    .Select("id") // only get the id
    .GetAsync();

这篇关于使用 MS Graph API C# 读取用户电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆