Issue with sending mails using Graph API

Krishna Nagesh Kukkadapu 21 Reputation points
2020-09-24T22:08:53.887+00:00

Hello Team,

I am trying to send a mail using Graph API using asp.net core c# and i am getting the following error

ServiceException: Code: BadRequest
Message: Current authenticated context is not valid for this request. This occurs when a request is made to an endpoint that requires user sign-in. For example, /me requires a signed-in user. Acquire a token on behalf of a user to make requests to these endpoints. Use the OAuth 2.0 authorization code flow for mobile and native apps and the OAuth 2.0 implicit flow for single-page web apps.

I have created an Application Registration and also granted the following API permissions.
Microsoft Graph:

  • Directory.Read.All
  • Application.ReadWrite.OwnedBy
  • Mail.Send
    Azure AD:
  • Directory.Read.All
  • Application.ReadWrite.OwnedBy

I am using this code
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantID)
.WithClientSecret(clientSecret)
.Build();

ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);

GraphServiceClient graphClient = new GraphServiceClient(authProvider);

var message = new Message
{
Subject = "Meet for lunch?",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "The new cafeteria is open."
},
ToRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "myToEmail@Stuff .com"
}
}
},
CcRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "myCCEmail@Stuff .com"
}
}
}
};

        var saveToSentItems = true;  

        await graphClient.Me  
          .SendMail(message, saveToSentItems)  
          .Request()  
          .PostAsync();  

        return graphClient;  

Can someone help in fixing this issue.

Microsoft Security Microsoft Entra Microsoft Entra ID
{count} votes

2 answers

Sort by: Most helpful
  1. Alfredo Revilla - Upwork Top Talent | IAM SWE SWA 27,526 Reputation points Moderator
    2020-09-25T20:53:38.037+00:00

    You're targeting the /me endpoint which is not available using application credentials. You need to specify the user account that will be used to send the message.

    await graphClient.Users["<user id or upn>"]
        .SendMail(message, saveToSentItems)
        .Request()
        .PostAsync();
    

    --
    Please let us know if this answer was helpful to you. If so, please remember to mark it as the answer so that others in the community with similar questions can more easily find a solution.

    4 people found this answer helpful.

  2. Krishna Nagesh Kukkadapu 21 Reputation points
    2020-09-25T23:41:02.483+00:00

    Is there away where i can send mails on behalf of a shared o365 mailbox , where i am seeing the shared mailbox as a Group in AAD ?


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.