Microsoft Graph API SDK .NET 获取其他用户电子邮件的问题其他用户、电子邮件、问题、Graph

2023-09-06 17:23:13 作者:朕的16c

我正在使用从 NuGet (1.2) 下载的 Microsoft Graph SDK.我向 Azure AD 进行身份验证(使用 ADAL).

I am using the Microsoft Graph SDK as downloaded from NuGet (1.2). I authenticate to Azure AD (using ADAL).

我正在使用客户端凭据流(未作为任何特定用户进行身份验证)并且正在使用应用程序权限角色来访问资源.

I am using Client Credentials flow (not authenticated as any particular user) and am using Application Permission roles to access resources.

我们将使用一堆别名设置一个服务邮箱.别名是给客户端的.这样他们就可以通过电子邮件发送一个对他们来说具有有意义名称的地址.

We are going to set up one service mailbox with a bunch of aliases. The aliases are given to the clients. This is so they are emailing an address that has a meaningful name to them.

我的应用程序将作为服务运行,并定期扫描此收件箱中的新电子邮件.它应该找到收件人地址,并根据使用的别名,将电子邮件归档到与该客户相关的位置.

My app will run as a service, and routinely scan new emails in this inbox. It should find the To address, and depending on what alias was used, file the email in a location relevant to that client.

我想要的资源是:GET/users//messages

但是,SDK 中似乎没有针对它的方法.

However, there doesn't appear to be a method in the SDK for it.

我可以通过这个获得用户:

I can get users with this:

IGraphServiceUsersCollectionPage filteredUsers = 
                   graphApi.Users.Request()
                                 .Filter("userPrincipalName eq 'user@domain.com'")
                                 .GetAsync().Result;

当我遍历集合时,我可以看到用户有一个消息"属性,但它始终为空.

When I loop through the collection, I can see that the User has a 'Messages' property, but it is always null.

如果我使用 HttpClient 手动构建请求消息,我可以获得消息.

If I manually build a request message with HttpClient I can get the messages.

第二个问题是 Recipient 属性始终是邮箱的 userPrincipalName.如何获取发件人使用的别名?

The second problem is that the Recipient property is always the userPrincipalName of the mailbox. How can I get the alias that was used by the sender?

推荐答案

虽然您能够成功获取用户集合,但您必须发出另一个请求才能接收消息.这看起来像:

While you are able to get your collection of users successfully, you have to make another request to receive the messages. This would look something like:

IUserMessagesCollectionPage userMessages = 
             graphApi.Users["user_id"].Messages.Request()
             .GetAsync().Result;

要回答您的第二个问题,目前您无法通过 Graph API 访问原始收件人,但您可以通过 EWS 执行此操作.这是因为您只能通过 EWS 检索 SMTP 邮件标头.您可以阅读有关如何执行此操作的更多信息 这里.

To answer your second question, at this time you cannot access the original recipient through the Graph API, but you can do this through EWS. This is due to the fact that you can only retrieve the SMTP message headers through EWS. You can read more about how to do this here.

如果您认为在图表中这对您很有价值,我鼓励您将其发布到我们的 UserVoice.

If this is something you believe is valuable to you in the Graph, I would encourage you to post it in our UserVoice.

如果您想将电子邮件作为文件获取,您可以通过 SDK 简单地将正文作为字节获取:

If you want to get the email as a file, you can simply get the body as bytes through the SDK:

byte[] asBytes = Encoding.Unicode.GetBytes(message.Body.ToString());