使用 uploadsession 的 Microsoft Graph API 无法接收带有附件的电子邮件

问题描述

当我尝试使用 Graph API 使用 Uploadsession 时,没有收到带有附件的电子邮件。有人可以帮助我理解为什么会发生这种情况。我没有收到任何错误

      Message draft = await graphServiceClient.Users["UserID"].Messages.Request().AddAsync(email);
        //Message draft = graphServiceClient.Users["UserID"].Mailfolders.Drafts.Messages.Request().AddAsync(email);

        var stream = System.IO.File.Open(@"C:\attach\DYN28_6579332_33242556.csv",System.IO.FileMode.Open,FileAccess.Read,FileShare.None);
        var attachmentItem = new AttachmentItem
        {
            AttachmentType = AttachmentType.File,Name = "DYN28_6579332_33242556.csv",Size = stream.Length
        };
        var uploadSession = await graphServiceClient.Users["Userid"].Messages[draft.Id]
                                .Attachments
                                .CreateUploadSession(attachmentItem)
                                .Request()
                                .PostAsync();
        var maxSlicesize = 320 * 1024;
        var largeFileUploadTask = new LargeFileUploadTask<FileAttachment>(uploadSession,stream,maxSlicesize);
        IProgress<long> progress = new Progress<long>(prog => {
            Console.WriteLine($"Uploaded {prog} bytes of {stream.Length} bytes");
        });

            // Upload the file
            var uploadResult = await largeFileUploadTask.UploadAsync(progress);

            if (uploadResult.UploadSucceeded)
            {
            // The ItemResponse object in the result represents the
            // created item.
            //Console.WriteLine($"Upload complete,item ID: {uploadResult.ItemResponse.Id}");
            Console.WriteLine("upload completed");
            }

  Finally sending email with 
                                                                      
                                                                         
  await graphServiceClient.Users["userid"].Messages[draft.Id]
                                  .Send()
                                  .Request()
                                  .PostAsync();

解决方法

Graph API 中的单个请求限制为 4MB。要发送更大的附件,您需要首先针对电子邮件消息/日历事件创建一个上传会话,并在此会话的多个请求中上传您的附件。 AFAIK 每个较小的 POST 请求也需要保持在 4MB 限制以下。

您可以找到更详细的文档和示例演练 here

POST https://graph.microsoft.com/v1.0/me/messages/AAMkADI5MAAIT3drCAAA=/attachments/createUploadSession
Content-type: application/json

{
  "AttachmentItem": {
    "attachmentType": "file","name": "flower","size": 3483322
  }
}