skip to Main Content

I’m walking through a console app tutorial from Microsoft to deal with outlook mail here

I did everything as is, but when I try to test it I get this exception when the GET request is sent.

Content type text/html does not have a factory registered to be parsed.

The code:

public static Task<MessageCollectionResponse?> GetInboxAsync()
{
    // Ensure client isn't null
    _ = _userClient ??
        throw new System.NullReferenceException("Graph has not been initialized for user auth");

    return _userClient.Me
        // Only messages from Inbox folder
        .MailFolders["Inbox"]
        .Messages
        .GetAsync((config) =>
        {
            //The next two lines were added by me to try to fix the problem
            config.Headers.Clear();
            config.Headers.Add("Accept", "application/json");
            // Only request specific properties
            config.QueryParameters.Select = new[] { "from", "isRead", "receivedDateTime", "subject" };
            // Get at most 25 results
            config.QueryParameters.Top = 25;
            // Sort by received time, newest first
            config.QueryParameters.Orderby = new[] { "receivedDateTime DESC" };
        });
}

2

Answers


  1. I followed same link and registered one Azure AD application with Supported account type as below and enabled public client flows:

    enter image description here

    I have one tenant named DemoAAD having Azure AD Free license like below:

    enter image description here

    In my case, I got same error when I ran same code by signing in with user from above tenant:

    dotnet run
    2
    

    Response:

    enter image description here

    To resolve the error, update your Azure AD license to Premium
    and make sure to assign Office 365 license to your user account.

    I have another tenant with Azure AD Premium P2 license like below:

    enter image description here

    In above tenant, I assigned an active Office 365 license to one user account:

    enter image description here

    When I ran same code, I picked user account having active Office 365 license from Azure AD Premium P2 licensed tenant to login where I got consent prompt like this:

    enter image description here

    After accepting the consent, I entered 2 and got list of inbox messages successfully in console output like below:

    dotnet run
    2
    

    Response:

    enter image description here

    Reference:
    c# – Using Azure MS Graph API Service Account – Stack Overflow by Rukmini

    Login or Signup to reply.
  2. this error Content type text/html does not have a factory registered to be parsed means you got an html content as the response which always indicating the request got error.

    My suggestion to narrow down the issue is:

    public static Task<MessageCollectionResponse?> GetInboxAsync()
    {
        // Ensure client isn't null
        _ = _userClient ??
            throw new System.NullReferenceException("Graph has not been initialized for user auth");
    
        var a = await _userClient.Me.GetAsync();//test if graph Client is set correctly
        //checking if permission is enough to query messages and if the user account have Email account binding to user account
        var b = await _userClient.Me.Messages.GetAsync((requestConfiguration) =>
                {
                    requestConfiguration.QueryParameters.Select = new string []{ "sender","subject" };
                });
        //add break point here to see the error message.
        var res = await _userClient.Me
            .MailFolders["Inbox"]
            .Messages
            .GetAsync((config) =>
            {
                //The next two lines were added by me to try to fix the problem
                //config.Headers.Clear();
                //config.Headers.Add("Accept", "application/json");
                // Only request specific properties
                config.QueryParameters.Select = new[] { "from", "isRead", "receivedDateTime", "subject" };
                // Get at most 25 results
                //config.QueryParameters.Top = 25;
                config.QueryParameters.Orderby = new[] { "receivedDateTime DESC" };
            });
        return res;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search