使用Micrososft Graph发送带有大附件的电子邮件

时间:2020-03-25 20:24:17

标签: c# microsoft-graph-api

我正在尝试使用Microsoft.Graph 3.0.1版发送带有大量附件的电子邮件。 这里的问题是我无法创建上传会话。我在用于执行此操作的C#代码下面附加了该代码。 “电子邮件”是我在应用程序中使用的模型。我将所有内容都放在一个单独的方法中,以获取更好的概述。可以看出,首先我正在尝试创建草稿并分块上传附件。之后,我要发送电子邮件。在应该创建会话的时候,我收到错误消息:“不支持OData请求。”完整的响应可以在catch部分中看到。

我正在使用的范围是:User.Read Mail.ReadWrite Mail.Send Files.ReadWrite Files.ReadWrite.All Sites.ReadWrite.All

我在这里做错了什么? 如果需要更多信息,请告诉我。

    private async Task SendEmailWithLargeAttaachment(Email email)
    {
        //The large attachment
        AttachmentItem attachmentItem = new AttachmentItem
        {
            Name = email.Attachments[0].FileName,
            AttachmentType = AttachmentType.File,
            Size = email.Attachments[0].Stream.Length
        };

        var message = new Message
        {
            Subject = "Test",
            Importance = Importance.Low,
            Body = new ItemBody
            {
                ContentType = BodyType.Html,
                Content = "This is a test message for large files."
            },
            IsRead = false,
            From = new Recipient
            {
                EmailAddress = new EmailAddress
                {
                    Address = "emailAddressThatSendTheEmail@outlook.com"
                }
            },
            ToRecipients = new List<Recipient>()
            {
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address =  "emailAddressThatReceiveTheEmail@outlook.com"
                    }
                }
            }
        };

        //Create the draft where I want to Upload the large file as attachment
        Message draft = await _graphServiceClient
             .Users["emailAddressThatSendTheEmail@outlook.com"]
             .Messages
             .Request()
             .AddAsync(message);

        try
        {
            var stream = new MemoryStream(email.Attachments[0].Stream);

            var uploadSession = await _graphServiceClient.Me.Messages[draft.Id].Attachments.CreateUploadSession(attachmentItem).Request().PostAsync();
            var maxChunkSize = 320 * 1024; 
            var provider = new ChunkedUploadProvider(uploadSession, _graphServiceClient, stream, maxChunkSize);

            var chunkRequests = provider.GetUploadChunkRequests();
            var trackedExceptions = new List<Exception>();
            DriveItem itemResult = null;

            //upload the chunks
            foreach (var request in chunkRequests)
            {                   
                var result = await provider.GetChunkRequestResponseAsync(request, trackedExceptions);

                if (result.UploadSucceeded)
                {
                    itemResult = result.ItemResponse;
                }
            }
        }
        catch (Exception ex)
        {
            //Error 
            //{
            //    "error": {
            //        "code": "ErrorInvalidRequest",
            //        "message": "The OData request is not supported.",
            //        "innerError": {
            //                                "request-id": "7822047f-b5ca-4e93-b7ab-63715af04c71",
            //          "date": "2020-03-25T19:47:13"
            //        }
            //    }
            //}
        }
    }

0 个答案:

没有答案