Mail.Send 'ErrorAccessDenied' when sending email, Delegated permissions

Saadia Fatima 0 Reputation points Microsoft Employee
2023-09-09T03:08:31.7133333+00:00

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
		

Microsoft Security Microsoft Graph
{count} votes

4 answers

Sort by: Most helpful
  1. Manu Philip 20,206 Reputation points MVP Volunteer Moderator
    2023-09-09T04:26:26.4366667+00:00

    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--

    0 comments No comments

  2. Mohamed Abderrahmane 0 Reputation points
    2023-09-09T18:24:05.0966667+00:00

    It's crucial for non-interactive scenarios to use Application permissions, So use Application permission instead of Delegated permission.

    0 comments No comments

  3. CarlZhao-MSFT 46,371 Reputation points
    2023-09-11T03:14:07.9666667+00:00

    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.

    User's image

    The picture below is a quick test:

    User's image

    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.


  4. CarlZhao-MSFT 46,371 Reputation points
    2023-09-13T03:14:29.89+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.