Hi TH-4749,
I'm still far from the end, I've tried the following, but getting an Microsoft.Graph.Models.ODataErrors.ODataError: Exception of type 'Microsoft.Graph.Models.ODataErrors.ODataError,
and I'm not sure if I'm making errors on the request body, or something else.
Probably we're using the wrong approach...
We've registered on Azure, an account [let's call it "svc23xxx@bosscompany.it"].
It has a shared "fully permissioned" account that we "MySharedAccount@mycompany.it".
In the code below, I've used directly "MySharedAccount@mycompany.it" and not "svc23xxx@bosscompany.it", so probably that could be the mistake.
But at this point I'm wondering if we are still over the right path.
The objective is to send an email automatically from a Windows Daemon Service, a daily mail from the Account "MySharedAccount@mycompany.it", without any human interaction.
public async static Task MakeGraphCallAsync()
{
// The client credentials flow requires that you request the
// /.default scope, and preconfigure your permissions on the
// app registration in Azure. An administrator must grant consent
// to those permissions beforehand.
var scopes = new[] { "https://graph.microsoft.com/.default" };
// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
var tenantId = "here_my_tenant";
// Values from app registration
var clientId = "here_my_client”;
var clientSecret = "here_my_clientsecret";
// using Azure.Identity;
var options = new TokenCredentialOptions
{
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 user = graphClient.Users["MySharedAccount@mycompany.it"];
Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
Message = new Message
{
Subject = "Test Mail",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "Example Body",
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "nicola.surname@mycompany.it",
},
},
},
CcRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "alessandra.surname@mycompany.it",
},
},
},
},
SaveToSentItems = false,
};
try
{
await graphClient.Users["MySharedAccount@applied.it"].SendMail.PostAsync(requestBody);
}
catch (Exception ex)
{
System.Console.WriteLine(ex);
}
}