My project using Microsoft.Graph 5.15 version send an email with large attachment. I searched the web and find the below code. However there are some issues:
1.MessageItemRequestBuilder.Send cannot be use like a method
2.AttachmentsItemRequestBuilder.CreateUploadSession cannot be use like a method
There are my code snippet
var savedDraft = await _graphClient.Users[userId].Messages.PostAsync(draftMessage);
// Handle large attachments
foreach (var largeAttachment in largeAttachments)
{
var fileStream = File.OpenRead(largeAttachment.FullName);
var uploadSession = await _thisGraphClient.Users[userId]
.Messages[savedDraft.Id]
.Attachments
.CreateUploadSession(new AttachmentItem
{
AttachmentType = AttachmentType.File,
Name = largeAttachment.Name,
Size = fileStream.Length,
ContentType = "application/octet-stream"
})
.Request()
.PostAsync();
int maxSliceSize = 320 * 1024; // 320 KB - Change this to fit your needs
var fileUploadTask = new LargeFileUploadTask<FileAttachment>(uploadSession, fileStream, maxSliceSize);
var totalLength = fileStream.Length;
IProgress<long> progress = new Progress<long>(prog =>
{
Console.WriteLine($"Uploaded {prog} bytes of {totalLength} bytes");
});
try
{
var uploadResult = await fileUploadTask.UploadAsync(progress);
Console.WriteLine(uploadResult.UploadSucceeded ? "Upload complete" : "Upload failed");
}
catch (ServiceException ex)
{
Console.WriteLine($"Error uploading: {ex.ToString()}");
}
}
// Send the draft
await _thisGraphClient.Users[userId].Messages[savedDraft.Id].Send().Request().PostAsync();
Would someone tell me what should fix it. Thanks.