Get outlook email message details from message id and graph token in C#

SAC_535 36 Reputation points
2022-11-16T10:49:01.54+00:00

I have message id and Graph access token need to get message details from this in C# dotNet.

How to call below get method in C# to get message details
GET https://graph.microsoft.com/v1.0/User["User Id"]/Messages["Message id"]
Accept: application/json
Authorization: Bearer <token>

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,577 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Vicky Kumar (Mindtree Consulting PVT LTD) 1,156 Reputation points Microsoft Employee
    2022-11-16T12:43:16.7+00:00

    If you are trying to get the messages with C# code , you should try with below code

    GraphServiceClient graphClient = new GraphServiceClient( authProvider );  
      
    var message = await graphClient.Me.Messages["{message-id}"]  
    	.Request()  
    	.GetAsync();  
    

    Ref doc - https://learn.microsoft.com/en-us/graph/api/message-get?view=graph-rest-1.0&tabs=csharp

    Git - https://github.com/microsoftgraph/microsoft-graph-docs/blob/main/api-reference/beta/api/user-list-messages.md

    Hope this helps

    Thanks

    0 comments No comments

  2. Srinivasa Rao Darna 6,686 Reputation points Microsoft Vendor
    2022-11-16T13:31:07.923+00:00

    Hello @SAC_535 ,

    A sample snippet:

    var message = await graphClient.Users["{user-id}"].Messages["{message-id}"]  
    	.Request()  
    	.GetAsync();  
    

    References:
    msgraph-sdk-dotnet
    message-get
    choose-authentication-providers.

    Hope this helps.
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

  3. CarlZhao-MSFT 36,891 Reputation points
    2022-11-17T02:37:40.36+00:00

    Hi @SAC_535

    You can call the endpoint using the C# Graph SDK, and since /users/{user id} only supports application contexts, you need to use the client credentials flow to get a token. Please refer to the complete code:

    using Azure.Identity;   
    using Microsoft.Graph;  
    using Newtonsoft.Json;  
          
    var scopes = new[] { "https://graph.microsoft.com/.default" };  
      
    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 message = await graphClient.Users["{user id}"].Messages["{message id}"]  
          .Request()  
          .GetAsync();  
      
    Console.WriteLine("message:" + JsonConvert.SerializeObject(message));  
    

    261137-image.png


    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.