Have you tried granting Mail.Send
Application permission and grant Admin consent
--please don't forget to upvote
and Accept as answer
if the reply is helpful--
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi
I am trying to send email, using Graph API, without any user interaction. I have obtained Admin consent for the Mail.Send for my application, with delegated permissions. I am getting this error:
What are the steps to send email using Graph API and delegated permissions in a non-interactive way.
Thanks
Welcome to Microsoft Graph!
Connected via apponly access using ***********************************
Readme: https://aka.ms/graph/sdk/powershell
SDK Docs: https://aka.ms/graph/sdk/powershell/docs
API Docs: https://aka.ms/graph/docs
NOTE: You can use the -NoWelcome parameter to suppress this message.
Send-MgUserMail : Access is denied. Check credentials and try again.
Status: 403 (Forbidden)
ErrorCode: ErrorAccessDenied
Have you tried granting Mail.Send
Application permission and grant Admin consent
--please don't forget to upvote
and Accept as answer
if the reply is helpful--
It's crucial for non-interactive scenarios to use Application permissions, So use Application permission instead of Delegated permission.
Hi @Saadia Fatima
Sending messages without user involvement is only supported in the application context. You need to grant Mail.Send
application permissions to the calling app and obtain an access token using the daemon-based client credentials flow.
The picture below is a quick test:
Hope this helps.
If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.
Hi @Saadia Fatima
Of course, if you want to use delegated permissions to send emails and avoid interacting with the user in the browser, then using the ROPC flow is the only option, which allows the application to log in the user by directly handling the user's password.
Note that with delegated permissions you can only send mail on behalf of the logged-in user and not on behalf of other users.
POST https://graph.microsoft.com/v1.0/me/sendMail
Content-type: application/json
{
"message": {
"subject": "Meet for lunch?",
"body": {
"contentType": "Text",
"content": "The new cafeteria is open."
},
"toRecipients": [
{
"emailAddress": {
"address": "******@contoso.onmicrosoft.com"
}
}
],
"ccRecipients": [
{
"emailAddress": {
"address": "******@contoso.onmicrosoft.com"
}
}
]
},
"saveToSentItems": "false"
}
Hope this helps.
If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.