i get an InvalidOperationException when calling GraphServiceClient.Users[].SendMail.PostAsync()

Gideon EZ Access 0 Reputation points
2024-12-14T00:18:50.69+00:00

I have simple code like this:

			// Ensure client isn't null
			_ = _appClient ??
				throw new System.NullReferenceException("Graph has not been initialized for app-only auth");

			var requestBody = new SendMailPostRequestBody {
				Message = new Message {
					Subject = "Meet for lunch?",
					Body = new ItemBody {
						ContentType = BodyType.Text,
						Content = "The new cafeteria is open."
					},
					ToRecipients = new List<Recipient> { 
						new Recipient {
							EmailAddress = new EmailAddress {
								Address = "******@contoso.com"
							}
						}
					}
				}
			};

			await _appClient.Users[_msGraphSettings.UserId].SendMail.PostAsync(requestBody);

I get a InvalidOperationException on the PostAsync():

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

requestBody is of type Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody

I am using App Only Auth. Examples in the documentation only show variants of Me.SendMail. This gives its own errors when using with App Only Auth.

What am i doing wrong? And why is it so hard to find any documentation on this?

Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-12-16T07:47:30.3766667+00:00

    Hello Gideon EZ Access,

    Thank you for reaching out to Microsoft Support!

    For your problem, we have conducted a test and it runs well. The cause of your error should be the import of the wrong package. My code is as follows, please refer to it:

    // The client credentials flow requires that you request the
    // /.default scope, and pre-configure your permissions on the
    // app registration in Azure. An administrator must grant consent
    // to those permissions beforehand.
    using Azure.Identity;
    using Microsoft.Graph;
    using Microsoft.Graph.Models;
    using Microsoft.Graph.Users.Item.SendMail;
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    // Values from app registration
    var clientId = "client";
    var tenantId = "tenantId";
    var clientSecret = "clientSecret";
    // using Azure.Identity;
    var options = new ClientSecretCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
    };
    // https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret, options);
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    var requestBody = new SendMailPostRequestBody
    {
        Message = new Message
        {
            Subject = "Meet for lunch?",
            Body = new ItemBody
            {
                ContentType = BodyType.Text,
                Content = "The new cafeteria is open.",
            },
            ToRecipients = new List<Recipient>
            {
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = "******@xxxxx.onmicrosoft.com",
                    },
                },
            }
        },
        SaveToSentItems = false,
    };
    await graphClient.Users["******@xxxx.OnMicrosoft.com"].SendMail.PostAsync(requestBody);
    

    Reference document:

    https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=csharp

    Hope this helps.

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

    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.