I tested locally and it worked fine. Please refer to my code:
using Azure.Identity;
using Microsoft.Graph;
using Newtonsoft.Json;
var scopes = new[] { "https://graph.microsoft.com/.default" };
// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
var tenantId = "tenant id";
// Values from app registration
var clientId = " client id";
var clientSecret = "client secret";
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var messages = await graphClient.Users["xxxxxxxxx.onmicrosoft.com"].MailFolders["Inbox"].Messages
.Request()
.Filter("isRead eq false and from/emailAddress/address eq '******@xxxxxxx.onmicrosoft.com' and subject eq 'This is a test message'")
.Select("body,subject,sender,from")
.Top(1000)
.GetAsync();
Console.WriteLine("messages:" + JsonConvert.SerializeObject(messages));
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.