Using latest Microsoft Graph examples here, with authProvider "Client Credentials Provider" here
Previously I've viewed a similar question Unable to send email via Microsoft Graph API (C# Console) but I'm still unable to make it work using the latest examples.
My Azure App registration has following API permissions:
I use code using .NET 7.0 and C#:
namespace EmailGraphTesting
{
public class Program
{
static async Task Main(string[] args)
{
await SendEmail.SendEmailAsync();
}
}
}
I'm calling:
using Microsoft.Graph;
using Azure.Identity;
using Microsoft.Graph.Models;
using Microsoft.Graph.Me.SendMail;
namespace EmailGraphTesting
{
public class SendEmail
{
public static string tenantId = "<tenant Id>";
public static string clientId = "<client Id>";
public static string clientSecret = "<client secret>";
public static string[] scopes = new[] { "https://graph.microsoft.com/.default" };
public static async Task SendEmailAsync()
{
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 = "Outbox Test",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "1st attempt"
},
ToRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "<receiverEmail here>"
}
}
},
},
};
await graphClient.Me.SendMail.PostAsync(requestBody);
}
}
}
I get the following error when running the code:
I also tried using
await graphClient.Users["<email here>"].SendMail.PostAsync(requestBody);
on the last line to no avail.
I see you are using Client Credentials Provider to send email but it looks like Me.SendMail
doesn't support Client Credentials Provider instead it uses Authorization Code Flow Provider as per this SO-Thread.
Thanks @Tiny Wang for the comment, you can use Users["{id or userPrincipalName}"].SendMail
.
Please grant Mail.Send
application permission to the registered app and use the below code, it will work for you.
using Microsoft.Graph;
using Azure.Identity;
using Microsoft.Graph.Models;
using Microsoft.Graph.Users.Item.SendMail;
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "{tenant_id}";
// Values from app registration
var clientId = "{client_id}";
var clientSecret = "{client_Secret}";
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var accessToken = await clientSecretCredential.GetTokenAsync(new Azure.Core.TokenRequestContext(scopes) { });
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 = "{Recipient email address}",
},
},
},
},
SaveToSentItems = false,
};
await graphClient.Users["*****@*****.onmicrosoft.com"].SendMail.PostAsync(requestBody);