I want to send email from service account through delegated permission, I tried two approaches but getting error while sending email:
Approach 1:
Approach IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create("ClientId")
.WithTenantId("TenantId")
.Build();
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication);
GraphServiceClient _graphClient = new GraphServiceClient(authProvider);
await _graphClient.Me
.SendMail(message, true)
.Request().WithUsernamePassword("ServiceAccountUser", "ServiceAccountUserPassword")
.PostAsync().ConfigureAwait(false);
Error: The given key 'Microsoft.Graph.AuthenticationHandlerOption' was not present in the dictionary.
Approach 2:
string[] scopes = { "Mail.Send" };
var options = new UsernamePasswordCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
var userNamePasswordCredential = new UsernamePasswordCredential(
"ServiceAccountUser", "ServiceAccountUserPassword", tenantId, clientId, options);
var graphClient = new GraphServiceClient(userNamePasswordCredential, scopes);
await graphClient.Me
.SendMail(message, true)
.Request()
.PostAsync().ConfigureAwait(false);
Error: Code: generalException
Message: An error occurred sending the request.
UsernamePasswordCredential authentication failed: AADSTS65001: The user or administrator has not consented to use the application with ID 'DumyId' named 'DumyName'. Send an interactive authorization request for this user and resource.
Added Permission:
Query: I want to send an email from my Azure Web application from Service Account through MS Graph, can you please help me to move forward ?