protected virtual void SendMicrosoftGraph(EmailMessageEntity email, MicrosoftGraphEmbedded microsoftGraph) { ClientCredentialProvider authProvider = microsoftGraph.GetAuthProvider(); GraphServiceClient graphClient = new GraphServiceClient(authProvider); var message = ToGraphMessage(email); graphClient.Users[email.From.AzureUserId.ToString()] .SendMail(message, false) .Request() .PostAsync().Wait(); }
public static ClientCredentialProvider GetAuthProvider(this MicrosoftGraphEmbedded microsoftGraph, string[]?scopes = null) { if (microsoftGraph.UseActiveDirectoryConfiguration) { return(AuthLogic.Authorizer is ActiveDirectoryAuthorizer ada?ada.GetConfig().GetAuthProvider() : throw new InvalidOperationException("AuthLogic.Authorizer is not an ActiveDirectoryAuthorizer")); } IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder .Create(microsoftGraph.Azure_ApplicationID) .WithTenantId(microsoftGraph.Azure_DirectoryID) .WithClientSecret(microsoftGraph.Azure_ClientSecret) .Build(); var authResultDirect = confidentialClientApplication.AcquireTokenForClient(scopes ?? new string[] { "https://graph.microsoft.com/.default" }).ExecuteAsync().Result; //Microsoft.Graph.Auth is required for the following to work ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication); return(authProvider); }