C# Code to Read Office 365 Emails.

Floyd Gladden 25 Reputation points
2025-02-17T11:44:43.39+00:00

My code is running null values on User Profile and Emails from Office 365. I have set up User.Read and Mail.Read Permissions

Microsoft Security | Microsoft Entra | Microsoft Entra ID
{count} votes

Accepted answer
  1. SrideviM 5,710 Reputation points Microsoft External Staff Moderator
    2025-02-25T12:19:32.3366667+00:00

    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:

    enter image description here

    Now, I registered one application and granted both User.Read.All and Mail.Read permission of Application type like this:

    enter image description here

    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:

    enter image description here


    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.

    User's image

    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.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.