I need to create an application that sends emails by MS Graph but also I need somehow restrict it for few mailboxes who will sending email (e.x. avoid send mail as ceo). Before I used just Sytem.Net.Mail and because basic authentication is now deprecation I must find new way to sending mails.
So I registered new application AAD, I added API permission for MS Graph Mail.Send (application type). Now I want to add restricting for that Graph API (I want to limit who can send a message from this API. I found that I must use New-ApplicationAccessPolicy cmdlet, but before that I created Mail-enabled security group. Then via PowerSell I addes new policy:
New-ApplicationAccessPolicy -AppId "9e48a326-a952-42ca-882f-ff1eec699ba7" -PolicyScopeGroupId "SMTPOAuth2SecurityGroup@consto.onmicrosoft.com" -AccessRight RestrictAccess -Description "SMTP OAuth2 Connector"
Then I added two accounts AlexW and DiegoS - both are from Microsoft 365 Developer Program, so both were not modify by me in any way:
Test-ApplicationAccessPolicy -Identity "AlexW@consto.onmicrosoft.com" -AppId "9e48a326-a952-42ca-882f-ff1eec699ba7" AppId : 9e48a326-a952-42ca-882f-ff1eec699ba7 Mailbox : AlexW AccessCheckResult : Granted
Test-ApplicationAccessPolicy -Identity "DiegoS@consto.onmicrosoft.com" -AppId "9e48a326-a952-42ca-882f-ff1eec699ba7" AppId : 9e48a326-a952-42ca-882f-ff1eec699ba7 Mailbox : DiegoS AccessCheckResult : Granted
But now I test my application. AlexW can send mail but for DiegoS (or random person) I got erorr:
DiegoS@consto.onmicrosoft.com:Code: ErrorAccessDenied Message: Access to OData is disabled. ClientRequestId: 909c72f7-02b7-4697-afd5-3d65a58d47a5
I try to remove and again add, wait some time and still the same problem. So, I need to create an application that sends emails by MS Graph but aslo I need somehow restrict