0

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 Answer 1

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"}

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I thought I had tried that but I missed the @ sign at the start of 'odata.type'. It is working fine after putting that in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.