Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,773 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Mail.Send
Application permissions
noreply@bbimanagementportaldev.onmicrosoft.com
/users
just fine.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
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.
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.