我正在尝试通过 Microsoft Graph 发送附件大于 4MB 的电子邮件。在网上搜索我得出的结论是,对于大于 4MB 的文件,我需要通过上传会话将文件上传到 onedrive,然后将其作为“参考附件”添加到电子邮件中。
到目前为止,我的代码如下所示,首先我将文件上传到一个驱动器
var graphClient = GetGraphClient();
var rootItem = graphClient.Users[userEmail].Drive.Root.Request().GetAsync().Result;
uploadSession = graphClient.Users[userEmail].Drive.Items[rootItem.Id].ItemWithPath(fullFileName).CreateUploadSession().Request().PostAsync().Result;
fullFileByteArray = Convert.FromBase64String(content);
stream = new MemoryStream(fullFileByteArray);
provider = new ChunkedUploadProvider(uploadSession, graphClient, stream);
driveItem = provider.UploadAsync(3).Result;
这工作得很好,我将文件上传到一个驱动器并获得所述文件的 ID。下一步是将电子邮件创建为草稿。
var email = await graphClient.Users[fromEmail].Messages.Request().AddAsync(message);
这也很好用,我将电子邮件创建为草稿(我可以在 Outlook 上看到它),并且我得到了所述附件的 ID。
现在到有问题的部分,当我尝试将附件添加到草稿电子邮件时。
foreach (var att in fileAttachments)
{
var attachment = new ReferenceAttachment();
attachment.Name = att.FullFileName;
attachment.ContentType = att.MIMEType;
attachment.Id = att.FileId;
attachment.ODataType = "#microsoft.graph.referenceAttachment";
attachment.Size = att.Size;
attachmentsParsed.Add(attachment);
await graphClient.Users[fromEmail].Messages[email.Id].Attachments.Request().AddAsync(attachment);
}
当我尝试执行 AddAsync() 它响应:
Message: The property 'SourceUrl' is required when creating the entity.
Inner error:
AdditionalData:
request-id: {{someId}}
date: {{someDate}}
ClientRequestId: {{someId}}
) ---> Microsoft.Graph.ServiceException: Code: ErrorInvalidProperty
Message: The property 'SourceUrl' is required when creating the entity.
Inner error:
AdditionalData:
request-id: {{someId}}
date: {{someDate}}
ClientRequestId: {{someId}}
问题是 ReferenceAttachment 没有 SourceUrl 属性,我也没有在上传文件的响应中找到 sourceUrl 参数。我尝试将它添加到附件的 AdditionalData 属性中,这是一个字典,但它不起作用。
还尝试像这样通过 Postman 发送请求(也尝试将其发送到 API 的 beta 版本):
{
"name":"{{someName}}",
"@odata.type": "#microsoft.graph.referenceAttachment",
"sourceUrl": "{{someUrl}}",
"contentType":"text/plain",
"id":"{{someId}}",
"size":"1553",
}
回应是
{
"error": {
"code": "ErrorInvalidProperty",
"message": "The property 'SourceUrl' is required when creating the entity.",
"innerError": {
"request-id": "{{someId}}",
"date": "{{someDate}}"
}
}
}
我在哪里添加这个 SourceUrl 属性,我从哪里得到它?