微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用 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
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。