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.