Get details of selected outlook email message details, messageId using GraphClient in C#

SAC_535 36 Reputation points
2022-03-10T05:54:49.7+00:00

I am using Graph client to get user outlook mailbox email message details.

Need help to get outlook selected message details, message Id using Graph client in C#.

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

3 answers

Sort by: Most helpful
  1. CarlZhao-MSFT 40,311 Reputation points
    2022-03-10T06:42:42.407+00:00

    Hi @SAC_535

    I wrote a C# sample for you that gets the details of a message by message id. I'm using the client credential flow to get the token, before that you need to grant the Mail.ReadBasic.All application permission to your application.

    using Microsoft.Graph;  
    using Microsoft.Graph.Auth;  
    using Microsoft.Identity.Client;  
    using Newtonsoft.Json;  
      
      
    namespace test1  
      
    {  
        class Program  
        {  
            static async System.Threading.Tasks.Task Main(string[] args)  
            {  
      
                IConfidentialClientApplication app;  
                app = ConfidentialClientApplicationBuilder.Create("{client id}")  
                        .WithClientSecret("{client secret}")  
                        //.WithRedirectUri("https://jwt.ms")  
                        .WithAuthority(new Uri("https://login.microsoftonline.com/{tenant id}"))  
                        .Build();  
      
                AuthenticationResult result;  
      
                string[] scopes = new string[] { "https://graph.microsoft.com/.default" };  
      
                result = await app.AcquireTokenForClient(scopes).ExecuteAsync();  
      
                /*string accesstoken = result.AccessToken;  
          
                Console.WriteLine(accesstoken);*/  
      
                ClientCredentialProvider authProvider = new ClientCredentialProvider(app);  
      
                GraphServiceClient graphClient = new GraphServiceClient(authProvider);  
      
                var message = await graphClient.Users["{user id}"].Messages["{message id}"]  
        .Request()  
        .GetAsync();  
      
                Console.WriteLine("message:" + JsonConvert.SerializeObject(message));  
      
            }  
        }  
    }  
    

    181762-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.


  2. SAC_535 36 Reputation points
    2022-03-10T08:22:00.047+00:00

    Thanks for reply.

    I know this method to get message details using message id. But my question is if I select any particular message from outlook inbox, sent Items then how to get message id of that selected email message.

    In attached file selected email message shown need to get message id of that selected email message using graph client in C#

    181814-m365.jpg


  3. CarlZhao-MSFT 40,311 Reputation points
    2022-03-11T02:53:38.483+00:00

    Hi @SAC_535

    It will indeed return multiple messages if the subject is the same. After my research found a better way, you can use internetMessageId to filter the information of the message, MessageId is specific and you can use it to get the complete information of the message.

    Follow the procedure below:

    1.Double-click an email message to open it outside of the Reading Pane.
    2.Click Tags>arrow symbol.

    182028-image.png

    https://graph.microsoft.com/v1.0/me/messages?$filter=internetMessageId eq '{Message id}'  
    

    182029-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.