Mark a Mail as Read/Unread not working | Graph API | C#

Th3TV 46 Reputation points
2022-10-06T14:19:13.233+00:00

I want to mark my mails as read/unread via the Graph API, but right now I have a code which does not work.

The Mail should update in the MailClient, like when you open to read it.

Can anyone tell me how to fix this issue or does anyone have a better code example for me?
Error code: 248059-grafik.png
Program.cs

        mailHelper.Init("{TenantID}", "{ClientID}", "{ClientSecret}");  
        var user = mailHelper.GetUser("{UserMail}");  
        var emailsOfUser = mailHelper.GetEmails(user.Id);  
  
  
foreach (var curMail in emailsOfUser)  
        {  
          try  
          {  
            mailHelper.MessageMarkReaOrUnRead(curMail.Id, true); /// todo  
          }  
          catch (Exception exLoopMsg)  
          {  
            Console.WriteLine(exLoopMsg.Message);  
          }  
        }  

Mail.cs

/// <summary>  
    ///   
    /// </summary>  
    /// <param name="messageIds"></param>  
    /// <param name="markAsRead"></param>  
    /// <param name="scopes"></param>  
    /// <returns></returns>  
    public bool? MessageMarkReaOrUnRead(List<string> messageIds, bool markAsRead, string[] scopes = null)  
    {  
      return MessageMarkReaOrUnReadAsync(messageIds, markAsRead, scopes).GetAwaiter().GetResult();  
    }  
  
    /* ------------------------------------------------------------ */  
    /// <summary>  
    ///   
    /// </summary>  
    /// <param name="messageIds"></param>  
    /// <param name="markAsRead"></param>  
    /// <param name="scopes"></param>  
    /// <returns></returns>  
    public bool? MessageMarkReaOrUnRead(string messageId, bool markAsRead, string[] scopes = null)  
    {  
      List<string> messageIds = new List<string>();  
      messageIds.Add(messageId);  
      return MessageMarkReaOrUnReadAsync(messageIds, markAsRead, scopes).GetAwaiter().GetResult();  
    }  
  
    /* ------------------------------------------------------------ */  
  
    private async Task<bool?> MessageMarkReaOrUnReadAsync(List<string> messageIds, bool markAsRead, string[] scopes = null)  
    {  
      GraphServiceClient graphServiceClient = this.GetAuthenticatedGraphClient(scopes);  
      bool? result;  
  
      if (markAsRead == true)  
      {  
        result = await graphServiceClient.Admin.ServiceAnnouncement.Messages  
          .MarkRead(messageIds)  
          .Request()  
          .PostAsync();  
      }  
      else  
      {  
        result = await graphServiceClient.Admin.ServiceAnnouncement.Messages  
          .MarkUnread(messageIds)  
          .Request()  
          .PostAsync();  
      }  
        
      return result;  
    }  
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,582 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,239 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 48,046 Reputation points
    2022-10-06T15:14:03.093+00:00

    This is the English version of the forums. Can you please translate the error message information that you're getting into English so we know what it says. The general gist appears to just be an unsuccessful HTTP status code so you should probably consider doing this test using Postman or curl or similar tool so you can see the raw HTTP request and response.