Microsoft Graph API SDK .NET Issues getting other

2019-03-02 07:49发布

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.

The resource I want is: GET /users/<id | userPrincipalName>/messages

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.

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

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?

1条回答
可以哭但决不认输i
2楼-- · 2019-03-02 08:39

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;

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.

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

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());
查看更多
登录 后发表回答