acess mailbox in graphapi

abhishek appu 41 Reputation points
2022-11-28T04:58:11.81+00:00

using Azure.Identity;
using Microsoft.Graph;
using System.Configuration;
using System.Threading.Tasks;

namespace Demo
{
class Program
{
static void Main(string[] args)
{
ReadEmail();

    }  
    public static void ReadEmail()  
    {  
        var scopes = new[] { "https://graph.microsoft.com/.default" };  
        var tenantId = ConfigurationManager.AppSettings["tenantId"];  
        var appId = ConfigurationManager.AppSettings["appId"];  
        var clientSecret = ConfigurationManager.AppSettings["clientSecret"];  
        var clientSecretCredential = new ClientSecretCredential(  
            tenantId, appId, clientSecret);  
        var graphClient = new GraphServiceClient(clientSecretCredential, scopes);  
        //var inboxMessages =  graphClient  
        //                        .Users["******@vidyatech.in"]  
        //                        .MailFolders["inbox"]  
        //                        .Messages  
        //                        .Request()  
        //                        .Expand("attachments")  
        //                        .Top(20)  
        //                        .GetAsync();  
        var inboxMessages = graphClient  
                                .Users["******@vidyatech.in"]  
                                .MailFolders["inbox"]  
                                .Messages  
                                .Request()  
                                .GetAsync();  

*****error=Id = 39, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}" why so?

Microsoft Security | Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. Zehui Yao_MSFT 5,876 Reputation points
    2022-12-02T09:52:00.377+00:00

    Hi @abhishek appu , I couldn't reproduce this issue on my end. Here's the code I ran successfully locally, hoping it would help you.

    using Azure.Identity;  
    using Microsoft.Graph;  
      
    class Program  
    {  
        static async Task Main(string[] args)  
        {  
            await ReadEmail();  
        }  
      
        static async Task ReadEmail()  
        {  
            var scopes = new[] { "https://graph.microsoft.com/.default" };  
      
            var tenantId = "{YOUR_TENANTID}";  
      
            var clientId = "{YOUR_CLIENTID}";  
      
            var clientSecret = "{YOUR_CLIENTSECRET}";  
      
            var clientSecretCredential = new ClientSecretCredential(  
                tenantId, clientId, clientSecret);  
      
            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);  
      
            var messages = await graphClient.Users["USER_ID"].MailFolders["Inbox"].Messages  
                .Request()  
                .GetAsync();  
      
            foreach (var message in messages.CurrentPage)  
            {  
                Console.WriteLine($"Message: {message.Subject ?? "NO SUBJECT"}");  
                Console.WriteLine($"From: {message.From?.EmailAddress?.Name}");  
                Console.WriteLine($"Status: {(message.IsRead!.Value ? "Read" : "Unread")}");  
                Console.WriteLine($"Received: {message.ReceivedDateTime?.ToLocalTime().ToString()}");  
            }  
        }  
    }  
    

    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.

    0 comments No comments

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.