- 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