'In SendEmail MSGRAPH API InternalServerError,Object reference not set to an instance of an object

Tried to use Microsoft Graph Email API, BUT getting following error.

ODATA Payload

"message": {
"subject": "Send email",
"body": {
"contentType": "text",
"content": "wuirbmndf"
},
"toRecipients": [
{
"emailAddress": {
"address": "emailid"
}
}
],
"attachments": [
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "attachment.txt",
"contentType": "text/plain",
"contentBytes": "SGVsbG8gV29ybGQh"
}
],
"from": {
"emailAddress": {
"address": "sender email id"
}
}

}

My Code Looks Like ->

client.BaseUrl = new Uri(String.Format("https://graph.microsoft.com/v1.0/me/sendmail"));
RestRequest request = new RestRequest();
request.Method = Method.POST;
request.AddParameter("Authorization", string.Format("Bearer " + emailObj.AccessToken), ParameterType.HttpHeader);
request.AddParameter("application/json", json, ParameterType.RequestBody);
//request.AddHeader("ContentType", "application/json");
//request.JsonSerializer.ContentType = "string";

var result = client.Execute(request);

And I am Getting InternalServerError with error Object reference not set to an instance of an object.


    {
  "error": {
    "code": "InternalServerError",
    "message": "Object reference not set to an instance of an object.",
    "innerError": {
      "request-id": "22ec4d88-2140-497f-bf28-80c83d7aa6a9",
      "date": "2020-05-27T04:24:14"
    }
  }
}


thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source