none
Microsoft Graph API, sending email response: StatusCode: 401 ReasonPhrase: 'Unauthorized' RRS feed

  • Question

  • I am trying to send an email using the Microsoft Graph API, and getting the following response:

    StatusCode: 401, ReasonPhrase: 'Unauthorized'...
    

    I'm trying to find out if there's something wrong with my code, or if the user account is lacking permissions.

    Here is the code below:

    string accessToken = GetADToken();
                string graphRequest = "https://graph.microsoft.com/v1.0/me/sendMail";
    
                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                // client.DefaultRequestHeaders.Add("ContentType", "application/json");
    
                var email = new
                {
                    message = new
                    {
                        subject = "test subject",
                        body = new
                        {
                            contentType = "Text",
                            content = "The cafeteria is open"
                        },
                        toRecepients = new List<object> { new {
                                emailAddress = new {
                                    address = "test.user@emailprovider.com"
                                }
                            }
                        },
                        ccRecipients = new List<object> { new {
                                emailAddress = new {
                                    address = "test.user@emailprovider.com"
                                }
                            }
                        }
                    },
                    saveToSentItems = false
                };
    
                // var requestContent = new StringContent(JsonConvert.SerializeObject(email), Encoding.UTF8, "application/json");
    
                string requestContent = JsonConvert.SerializeObject(email);
                var buffer = Encoding.UTF8.GetBytes(requestContent);
                var byteContent = new ByteArrayContent(buffer);
                byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
    
                var response = client.PostAsync(graphRequest, byteContent).Result;
    
                return response;


    Wednesday, September 5, 2018 12:37 PM

All replies

  • Hi Rommel John,

    Since your question is more related to Microsoft Graph API, you could post a new thread in GitHub Issues.

    https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 6, 2018 8:34 AM