Hello Floyd Gladden,
As per our discussion, your domain currently has only one user (your personal account as a guest user). To successfully retrieve Office 365 emails using Microsoft Graph API, you need to follow below steps:
- Create new users within your domain in Azure Active Directory (AAD).
- Assign Office 365 licenses to those users.
- Once the licenses are assigned, you should be able to send and retrieve emails via Microsoft Graph API.
In my case, I have one user with active Office 365 E5 license assigned as below:
Now, I registered one application and granted both User.Read.All
and Mail.Read
permission of Application type like this:
When I ran below C# code, I got the mails of user successfully like this:
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
class Program
{
static async Task Main(string[] args)
{
string tenantId = "tenantId";
string clientId = "appId";
string clientSecret = "secret";
string userId = "******@xxxxxxxx.onmicrosoft.com";
try
{
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential);
Console.WriteLine("Fetching emails...");
var messages = await graphClient.Users[userId].Messages
.GetAsync(requestConfiguration =>
{
requestConfiguration.QueryParameters.Top = 10;
requestConfiguration.QueryParameters.Select = new string[] { "receivedDateTime", "subject", "from" };
requestConfiguration.QueryParameters.Orderby = new string[] { "receivedDateTime DESC" };
});
if (messages?.Value != null && messages.Value.Count > 0)
{
Console.WriteLine("Emails retrieved successfully:\n");
foreach (var message in messages.Value)
{
Console.WriteLine($"Received: {message.ReceivedDateTime}");
Console.WriteLine($"From: {message.From?.EmailAddress?.Address}");
Console.WriteLine($"Subject: {message.Subject}");
Console.WriteLine("---------------------------------------------------");
}
}
else
{
Console.WriteLine("No emails found.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error retrieving emails: {ex.Message}");
}
}
}
Response:
Please do not forget to click "Accept the answer” and Yes
wherever the information provided helps you, this can be beneficial to other community members.
If you have any other questions or still running into more issues, let me know in the "comments" and I would be happy to help you.