Hello.
I have a .Net Core API that needs to send notification mails.
To do this, an application with delegated permissions and a user for sending mail has been created in Azure AD.
The application has been granted mail.send permission for the graph api.
I have installed the Microsoft.Graph library version 3.21.0 in my API and I have created the following function to send notifications.
private static void SendMailMessageService(IConfiguration configuration, string template, string user)
{
Microsoft.Identity.Client.IConfidentialClientApplication clientApplication =
Microsoft.Identity.Client.ConfidentialClientApplicationBuilder
.Create(configuration["AzureMail:ClientId"])
.WithTenantId(configuration["AzureMail:TenantId"])
.WithClientSecret(configuration["AzureMail:ClientSecret"])
.Build();
Microsoft.Graph.Auth.OnBehalfOfProvider authProvider =
new Microsoft.Graph.Auth.OnBehalfOfProvider(clientApplication);
Microsoft.Graph.GraphServiceClient graphClient =
new Microsoft.Graph.GraphServiceClient(authProvider);
Microsoft.Graph.Message msg = new Microsoft.Graph.Message()
{
Subject = configuration["AzureMail:Subject"].ToString(),
Body = new Microsoft.Graph.ItemBody()
{
Content = template,
ContentType = Microsoft.Graph.BodyType.Html
},
ToRecipients = new List<Microsoft.Graph.Recipient>() {
new Microsoft.Graph.Recipient {
EmailAddress = new Microsoft.Graph.EmailAddress {
Address = user
}
}
}
};
var request = graphClient.Users[configuration["AzureMail:UserFrom"]].SendMail(msg, false).Request();
var result = request.PostAsync();
result.Wait();
}
When I run this function it is returning the following error:
{Status Code: 0
Microsoft.Graph.ServiceException: Code: generalException
Message: An error occurred sending the request.
---> Microsoft.Graph.Auth.AuthenticationException: Code: generalException
Message: Unexpected exception occurred while authenticating the request.
---> System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Identity.Client.Internal.Requests.OnBehalfOfRequest.GetBodyParameters()
at Microsoft.Identity.Client.Internal.Requests.OnBehalfOfRequest.ExecuteAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.ApiConfig.Executors.ConfidentialClientExecutor.ExecuteAsync(AcquireTokenCommonParameters commonParameters, AcquireTokenOnBehalfOfParameters onBehalfOfParameters, CancellationToken cancellationToken)
at Microsoft.Graph.Auth.OnBehalfOfProvider.GetNewAccessTokenAsync(AuthenticationProviderOption msalAuthProviderOption)
--- End of inner exception stack trace ---
at Microsoft.Graph.Auth.OnBehalfOfProvider.GetNewAccessTokenAsync(AuthenticationProviderOption msalAuthProviderOption)
at Microsoft.Graph.Auth.OnBehalfOfProvider.AuthenticateRequestAsync(HttpRequestMessage httpRequestMessage)
at Microsoft.Graph.AuthenticationHandler.SendAsync(HttpRequestMessage httpRequestMessage, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Microsoft.Graph.HttpProvider.SendRequestAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Microsoft.Graph.HttpProvider.SendRequestAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at Microsoft.Graph.BaseRequest.SendRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
at Microsoft.Graph.BaseRequest.SendAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)}
The idea is to send the notification email from the account indicated in configuration ["AzureMail: UserFrom"].
Could you help me? Do you know why this is giving me an error? Is it a problem with my code? Could it be influencing that this user has the MFA active?
I need to solve it as soon as possible since in a few days it will be the only way in which we can send notifications.
I hope your answer.
A greeting and thanks to all.