Microsoft Graph Rest API Add attachment to email using c# ASP.NET MVC

走远了吗. 提交于 2019-12-12 03:26:56

问题


I am attempting to use Microsoft Graph to send an email with an attachment, however I am only getting back a response of 'Internal Server Error'. I have tried several different things but am having no joy so hopefully someone here can help!

The API states that you can create and send an email with attachment, but having had issues with that before I am trying to first create the email as a draft, then add the attachment to it, and finally send it. The draft creates fine, and without an attachment it send fine. Here is the section of code I am using to attach a file to the email:

            // Now add the attachments
        using (var client = new HttpClient())
        {
            // URL = https://graph.microsoft.com/v1.0/me/messages/{id}/attachments
            string AddAttachmentsUrl = GraphSettings.AddAttachmentsToMessageUrl;
            AddAttachmentsUrl = AddAttachmentsUrl.Replace("{id}", newMessageId);

            using (var request = new HttpRequestMessage(HttpMethod.Post, AddAttachmentsUrl))
            {
                request.Headers.Accept.Add(Json);
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                string serializedData = JsonConvert.SerializeObject(current);
                // serializedData = {\"Name\":\"Test.txt\",\"ContentBytes\":\"VGVzdA0K\",\"ContentType\":\"text/plain\"}
                request.Content = new StringContent(serializedData, Encoding.UTF8, "application/json");

                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        sendMessageResponse.Status = SendMessageStatusEnum.Fail;
                        sendMessageResponse.StatusMessage = response.ReasonPhrase;
                        return sendMessageResponse;
                    }
                }
            }
        }

I have put a couple of comments in so you can see the URL I am posting to and also the contents of the object I am attempting to POST. I am sure I'm missing something obvious, maybe to do with encoding the bytes[] data somehow or setting the content type in a header somewhere?

Any help greatly appreciated, here is the link to the API function I am using: http://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/message_post_attachments


回答1:


As illustrated in the documentation, please ensure that the json payload contains "@odata.type" property with value as "#microsoft.graph.fileAttachment". Below the payload quoted in the question with the change (in bold).

{"@odata.type":"#microsoft.graph.fileAttachment","Name":"Test.txt","ContentBytes":"VGVzdA0K","ContentType":"text/plain"}



来源:https://stackoverflow.com/questions/35845541/microsoft-graph-rest-api-add-attachment-to-email-using-c-sharp-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!