微软图形REST API添加附件使用C#ASP.NET MVC的电子邮件 [英] Microsoft Graph Rest API Add attachment to email using c# ASP.NET MVC

查看:241
本文介绍了微软图形REST API添加附件使用C#ASP.NET MVC的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Microsoft图表来发送带有附件的电子邮件,但我只取回内部服务器错误的响应。我已经尝试了几种不同的东西,但我有没有快乐,所以希望这里有人能帮助!

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!

您可以创建和附件发送电子邮件,但在此之前,我想先创建电子邮件作为草稿,然后将附件添加到它有过的问题,并最终把它的API状态。草案创造的罚款,并没有附件发送的罚款。下面是我使用的文件附加到电子邮件中的code部分:

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

我已经把一对夫妇的意见中,所以你可以看到URL我张贴到,也是对象的内容我试图POST。我相信,我失去了一些东西明显,可能与编码字节[]数据以某种方式或在某处头设置内容类型呢?

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?

任何帮助非常AP preciated,这里是链接到我使用的API函数:的http://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/message_post_attachments

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

推荐答案

由于在的文档,请确保JSON有效载荷包含#microsoft.graph.fileAttachment@ odata.type以价值为财产。在下面的变化(粗体)问题所引述的有效载荷。

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

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

这篇关于微软图形REST API添加附件使用C#ASP.NET MVC的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆