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

Trevor Westin 26 Reputation points
2024-04-29T17:53:24.4566667+00:00
  • I am using a B2C tenant
  • I have an app registration with Mail.Send Application permissions
    • With granted Admin consent.
  • The user with id b29d1ccb-xxxxxxxxxxxx is an internal account with User principal name: noreply@bbimanagementportaldev.onmicrosoft.com
  • I can use the same function below to call /users just fine.
  • I've followed everything from sendMail docs + examples as closely as possible.

The error:

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

The code:

var scopes = new[] { "https://graph.microsoft.com/.default" };

        string tenantId = "xxxxxxxxxxxxxxxxxxxxx";
        string clientId = "xxxxxxxxxxxxxxxxxxxxx";
        string clientSecret = "xxxxxxxxxxxxxxxxxxxxx";

        var options = new ClientSecretCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
        };

        var clientSecretCredential = new ClientSecretCredential(
            tenantId, clientId, clientSecret, options);

        var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

        var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
        {
            Message = new Message
            {
                Subject = "Reset Password",
                Body = new ItemBody
                {
                    ContentType = BodyType.Html,
                    Content = "Please reset your password soon!",
                },
                ToRecipients = new List<Recipient>
                {
                    new Recipient
                    {
                        EmailAddress = new EmailAddress
                        {
                            Address = "twestin@bbilogistics.com",
                        },
                    },
                },
                From = new Recipient { EmailAddress = new EmailAddress { Address = "noreply@bbimanagementportaldev.onmicrosoft.com" } }
            },
        };

        try
        {
            await graphClient.Users["b29d1ccb-7ccc-xxxxx-xxxxx"].SendMail.PostAsync(requestBody);
            return true;
        }
        catch (ServiceException ex)
        {
            Console.WriteLine($"Error sending email: {ex.Message}");
            return false;
        }

In postman I can get an access token using client_credentials and use it to fetch all users from the tenant but I run into another error when I attempt to call /sendMail.: "The tenant for tenant guid does not exist."

Thank you

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

Accepted answer
  1. Yakun Huang-MSFT 475 Reputation points Microsoft Vendor
    2024-04-30T02:00:13.08+00:00

    Hi @Trevor Westin

    The reason why the tenant ID does not exist is that the tenant lacks the required MS 365 license, so you need to grant the tenant an MS 365 license.

    Screenshot 2024-04-30 094422

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    0 comments No comments

0 additional answers

Sort by: Most helpful