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

Trevor Westin 31 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.
11,935 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yakun Huang-MSFT 4,395 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

1 additional answer

Sort by: Most helpful
  1. Quentin GONEL 0 Reputation points
    2024-09-19T08:38:11.4566667+00:00

    Hello,

    I am encountering a similar issue with my application. I allow users to link their account to my application so they can send emails, but for one particular user, an error occurs: UnexpectedValueException: Content type text/html does not have a factory to be parsed.

    This user is external to my tenant, and I have invited them as an External User via Microsoft 365 administration. I would like to point out that I have several other external users who are working perfectly.

    I assume the issue might be related to the user themselves, and possibly to their license. However, I was using SMTP for sending emails before, and it worked fine for this user. So, is it really possible that the problem is related to the license? If not, what could be causing this issue?

    Thank you,

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.