msgraph-sdk-dotnet
msgraph-sdk-dotnet copied to clipboard
graphServiceClient.Me.SendMail.PostAsync(requestBody)
Describe the bug Using Graph v5.0.0 When attempting to send an email with the Microsoft.Graph.Me.SendMail method I'm getting the following error:
DemoGraph.Controllers.HomeController: Error: Error: Microsoft.Graph.Models.ODataErrors.ODataError: Exception of type 'Microsoft.Graph.Models.ODataErrors.ODataError' was thrown.
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.ThrowIfFailedResponse(HttpResponseMessage response, Dictionary2 errorMapping, Activity activityForAttributes) at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendNoContentAsync(RequestInformation requestInfo, Dictionary
2 errorMapping, CancellationToken cancellationToken)
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendNoContentAsync(RequestInformation requestInfo, Dictionary2 errorMapping, CancellationToken cancellationToken) at Microsoft.Graph.Me.SendMail.SendMailRequestBuilder.PostAsync(SendMailPostRequestBody body, Action
1 requestConfiguration, CancellationToken cancellationToken)
at DemoGraph.Graph.GraphServices.SendEmailMessage(String nameSendTo, GraphServiceClient graphServiceClient, String subject, String emailBody, ILogger log) in C:\Users\dadrape_adm\source\repos\DemoGraph\DemoGraph\Graph\GraphServices.cs:line 48
at DemoGraph.Controllers.HomeController.SendMail() in C:\Users\dadrape_adm\source\repos\DemoGraph\DemoGraph\Controllers\HomeController.cs:line 77
But when using the Microsoft.Graph.User.SendMail it working as expected
Expected behavior Email is sent from the current logged in user.
Screenshots
Desktop (please complete the following information):
- OS: Windows Server
- Browser : chrome
- Version: 110.0.5481.178
Additional context Add any other context about the problem here.
This is the Code: var requestBody = new Microsoft.Graph.Me.SendMail.SendMailPostRequestBody {
Message = new Message
{
Subject = subject,
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = emailBody
},
ToRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = nameSendTo
}
}
},
},
SaveToSentItems = true,
};
try
{
log.LogInformation($"In the try catch in send email");
// Send the email
// await graphServiceClient.Users[userName].SendMail.PostAsync(requestBody, null);
await graphServiceClient.Me.SendMail.PostAsync(requestBody);
}
Thanks for raising this @Dale-Dr
Any chance you can confirm the error you are receiving when you handle the API error as documented at the link below? https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/upgrade-to-v5.md#error-handling
Hi,
This is the error code and message:
Microsoft.Graph.Models.ODataErrors.MainError.Code.get returned "BadRequest" string Microsoft.Graph.Models.ODataErrors.MainError.Message.get returned "/me request is only valid with delegated authentication flow." String
I have added the API Permissions to the app.. @.***
From: Eastman @.> Sent: Monday, 6 March 2023 5:47 PM To: microsoftgraph/msgraph-sdk-dotnet @.> Cc: Dale A Draper @.>; Mention @.> Subject: Re: [microsoftgraph/msgraph-sdk-dotnet] graphServiceClient.Me.SendMail.PostAsync(requestBody) (Issue #1681)
Thanks for raising this @Dale-Drhttps://urldefense.com/v3/__https:/github.com/Dale-Dr__;!!OMLVqIk!w23KldZaEfJVBBteuqXEORGp_55y7ofkAF16yD4Gar2XXD4IdTTKs5z294CopJwW0pMxck71VOUAdnzdTLKfmhk2oEyFw3R_rAI$
Any chance you can confirm the error you are receiving when you handle the API error as documented at the link below? https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/upgrade-to-v5.md#error-handlinghttps://urldefense.com/v3/__https:/github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/upgrade-to-v5.md*error-handling__;Iw!!OMLVqIk!w23KldZaEfJVBBteuqXEORGp_55y7ofkAF16yD4Gar2XXD4IdTTKs5z294CopJwW0pMxck71VOUAdnzdTLKfmhk2oEyFO1thJtY$
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/microsoftgraph/msgraph-sdk-dotnet/issues/1681*issuecomment-1455626084__;Iw!!OMLVqIk!w23KldZaEfJVBBteuqXEORGp_55y7ofkAF16yD4Gar2XXD4IdTTKs5z294CopJwW0pMxck71VOUAdnzdTLKfmhk2oEyFfKrYJFU$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AJ76K6G76EGCX3OIPPKX3GDW2WI65ANCNFSM6AAAAAAVQQS7U4__;!!OMLVqIk!w23KldZaEfJVBBteuqXEORGp_55y7ofkAF16yD4Gar2XXD4IdTTKs5z294CopJwW0pMxck71VOUAdnzdTLKfmhk2oEyFZYpy7MQ$. You are receiving this because you were mentioned.Message ID: @.***>
WARNING: This email (including any attachments) may contain legally privileged, confidential or private information and may be protected by copyright. You may only use it if you are the person(s) it was intended to be sent to and if you use it in an authorised way. No one is allowed to use, review, alter, transmit, disclose, distribute, print or copy this email without appropriate authority.
If this email was not intended for you and was sent to you by mistake, please telephone or email me immediately, destroy any hard copies of this email and delete it and any copies of it from your computer system. Any right which the sender may have under copyright law, and any legal privilege and confidentiality attached to this email is not waived or destroyed by that mistake.
It is your responsibility to ensure that this email does not contain and is not affected by computer viruses, defects or interference by third parties or replication problems (including incompatibility with your computer system).
Opinions contained in this email do not necessarily reflect the opinions of the Department of Transport and Main Roads, or endorsed organisations utilising the same infrastructure.
Hey @Dale-Dr,
I believe the error you are getting is due to your app using application permissions rather than delegated permissions.
As the application context does not have a "user" that the application is acting as, you cannot call the /me
endpoint. To do this you would need to acquire a user token so that the /me
call is associated with that user.
To do this, you would need to authenticate using one of the public client flows that involves acquiring tokens for a user.
Hi Eastman,
Strange as I am logging into the app.
I have also have added the following delegated API Permissions: Mail.Send Mail.ReadWrite
From: Eastman @.> Sent: Tuesday, 7 March 2023 4:41 PM To: microsoftgraph/msgraph-sdk-dotnet @.> Cc: Dale A Draper @.>; Mention @.> Subject: Re: [microsoftgraph/msgraph-sdk-dotnet] graphServiceClient.Me.SendMail.PostAsync(requestBody) (Issue #1681)
I believe the error you are getting is due to your app using application permissions rather than delegated permissions. As the application context does not have a "user" that the application is acting as, you cannot call the /me endpoint. To do this you would need to acquire a user token so that the /me call is associated with that user.
To do this, you would need to authenticate using one of the public client flows that involves acquiring tokens for a user.
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/microsoftgraph/msgraph-sdk-dotnet/issues/1681*issuecomment-1457627150__;Iw!!OMLVqIk!xq91fhPBTZn87t_DA1Z5etEoB02KAmyjy8qpcVc4KTnbbhmH8CkNlAJQ_-c-nPK5Vc3Esl6SRsh43HqoK-RB3WcUZVpvr601Ly8$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AJ76K6A3P2TAUIE7CICKYWDW23KBLANCNFSM6AAAAAAVQQS7U4__;!!OMLVqIk!xq91fhPBTZn87t_DA1Z5etEoB02KAmyjy8qpcVc4KTnbbhmH8CkNlAJQ_-c-nPK5Vc3Esl6SRsh43HqoK-RB3WcUZVpv_cNCTiQ$. You are receiving this because you were mentioned.Message ID: @.***>
WARNING: This email (including any attachments) may contain legally privileged, confidential or private information and may be protected by copyright. You may only use it if you are the person(s) it was intended to be sent to and if you use it in an authorised way. No one is allowed to use, review, alter, transmit, disclose, distribute, print or copy this email without appropriate authority.
If this email was not intended for you and was sent to you by mistake, please telephone or email me immediately, destroy any hard copies of this email and delete it and any copies of it from your computer system. Any right which the sender may have under copyright law, and any legal privilege and confidentiality attached to this email is not waived or destroyed by that mistake.
It is your responsibility to ensure that this email does not contain and is not affected by computer viruses, defects or interference by third parties or replication problems (including incompatibility with your computer system).
Opinions contained in this email do not necessarily reflect the opinions of the Department of Transport and Main Roads, or endorsed organisations utilising the same infrastructure.
Question: how do we setup the "Authorization code provider". in the doco it only has this: // For authorization code flow, the user signs into the Microsoft // identity platform, and the browser is redirected back to your app // with an authorization code in the query parameters var authorizationCode = "AUTH_CODE_FROM_REDIRECT";
I can't seem to find any help documentation on how to set this up?
All of the examples I've found refer to using DelegateAuthenticationProvider, but this is no longer available...
Strange as I am logging into the app. I have also have added the following delegated API Permissions: Mail.Send Mail.ReadWrite
Any chance you are able to share a code sample of how you are creating the GraphServiceClient
instance to help us understand this better?
Any chance you are using one of the methods available at https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?view=graph-rest-1.0&tabs=CS
Hi,
I’m using the “Client credentials provider” option from the link you provided… I’ve realised that now it’s the wrong one so I’m now trying the “Authorization code provider” option. But I’m having issues finding help on how to get the “authorizationCode”, from this step: var authorizationCode = "AUTH_CODE_FROM_REDIRECT";
The setup for Graph is in the homeController
Hi Any advise on how to get the authorizationCode
Hi Dale, I've a similar issue. I'm still getting a bit confused on how and if is possibile to send an email, automatically from an application, that follows a specific autohization flow, without then a human interaction. I've successfully get Application token, for application only, get Delegated user access token, but in both cases, when then I try to send an email wtih graphClient.Me.SendMail.PostAsync(requestBody), I get that damned prompt for device authentication.
I'm wondering at this point, if still exists a way to send an email with Graph API, that will not require a real user to manually login when trying to send an email
Hi Any advise on how to get the authorizationCode
Any success on "AUTH_CODE_FROM_REDIRECT" 🤔
Hi,
No never got any advice on it….
From: dromer328 @.> Sent: Wednesday, 9 August 2023 4:19 AM To: microsoftgraph/msgraph-sdk-dotnet @.> Cc: Dale A Draper @.>; Mention @.> Subject: Re: [microsoftgraph/msgraph-sdk-dotnet] graphServiceClient.Me.SendMail.PostAsync(requestBody) (Issue #1681)
Hi Any advise on how to get the authorizationCode
Any success on "AUTH_CODE_FROM_REDIRECT" 🤔
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/microsoftgraph/msgraph-sdk-dotnet/issues/1681*issuecomment-1670089910__;Iw!!OMLVqIk!yDzOvBmIXGj1OCWHw1VoB1k9oXI8T0TSVh2Q1uVDt6GhnYWz5JyYzgEZzc_x1ZtOIscxHVFOYc-8d-KMa5UaqPni8gb0yiZqYKM$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AJ76K6FGKLSEJTXAUUTP3BTXUJ7JVANCNFSM6AAAAAAVQQS7U4__;!!OMLVqIk!yDzOvBmIXGj1OCWHw1VoB1k9oXI8T0TSVh2Q1uVDt6GhnYWz5JyYzgEZzc_x1ZtOIscxHVFOYc-8d-KMa5UaqPni8gb0aGU2h6c$. You are receiving this because you were mentioned.Message ID: @.@.>>
WARNING: This email (including any attachments) may contain legally privileged, confidential or private information and may be protected by copyright. You may only use it if you are the person(s) it was intended to be sent to and if you use it in an authorised way. No one is allowed to use, review, alter, transmit, disclose, distribute, print or copy this email without appropriate authority.
If this email was not intended for you and was sent to you by mistake, please telephone or email me immediately, destroy any hard copies of this email and delete it and any copies of it from your computer system. Any right which the sender may have under copyright law, and any legal privilege and confidentiality attached to this email is not waived or destroyed by that mistake.
It is your responsibility to ensure that this email does not contain and is not affected by computer viruses, defects or interference by third parties or replication problems (including incompatibility with your computer system).
Opinions contained in this email do not necessarily reflect the opinions of the Department of Transport and Main Roads, or endorsed organisations utilising the same infrastructure.