I need to read a certain email but a different account.
The first example works
string applicationClientID = "aaaaaaaaaaaaaaaaaaaa";
string directoryTenantID = "dddddddddddddddddd";
string secretID = "sssssssssssssssssssssss";
//Example 01: OK
string email = "[email protected]"; //WORKS: account
that will read the email = [email protected]
//Example 02: Error need to read emailRead@outlook with account [email protected]
string email = "[email protected]"; // DOES NOT WORK:
account that has permission to read email:
[email protected]
var credentials = new ClientSecretCredential(
directoryTenantID, applicationClientID, secretID,
new TokenCredentialOptions { AuthorityHost =
AzureAuthorityHosts.AzurePublicCloud });
GraphServiceClient graphServiceClient = new
GraphServiceClient(credentials);
var inboxMessages = await graphServiceClient
.Users[email]
.MailFolders["inbox"]
.messages
.Request()
.Expand("attachments")
.Top(20)
.GetAsync();
//I get the following message: Message: Access to OData is disabled.
2
Answers
I think your code should work, except
messages
should beMessages
. Here’s my test result.Firstly, when we want to check emails for a specific email account, we need to use client credential flow just like you used.
Limiting application permissions to specific Exchange Online mailboxes
Mail.Read
Mail.ReadBasic
Mail.ReadBasic.All
Mail.ReadWrite
Mail.Send
MailboxSettings.Read
MailboxSettings.ReadWrite
Calendars.Read
Calendars.ReadWrite
Contacts.Read
Contacts.ReadWrite
Test-ApplicationAccessPolicy
Get-ApplicationAccessPolicy
New-ApplicationAccessPolicy
Remove-ApplicationAccessPolicy
Set-ApplicationAccessPolicy