У меня возникают проблемы с большими вложениями при отправке электронной почты Библиотека Microsoft Graph 5 с использованием .NET 4.7.2
Фрагмент кода по ссылке:
Код: Выделить всё
public string SendEmailWithAttachments(GraphServiceClient graphClient, string objectID, string recipient, FileInfo littleAttachment, FileInfo bigAttachment)
{
var _Msg = string.Empty;
// Create message
var draftMessage = new Microsoft.Graph.Models.Message
{
Subject = "Large attachment test",
};
draftMessage.ToRecipients = new List() { new Recipient() { EmailAddress = new EmailAddress() { Address = recipient } } };
//---------------------------------------------------Little Attachments-------------------------------------------------------
byte[] littleStream = File.ReadAllBytes(littleAttachment.FullName);
draftMessage.Attachments = new List
{
new Microsoft.Graph.Models.Attachment
{
OdataType = "#microsoft.graph.fileAttachment",
Name = littleAttachment.Name,
AdditionalData = new Dictionary
{
{
"contentBytes", Convert.ToBase64String(littleStream)
}
}
}
};
//---------------------------------------------------Little Attachments End---------------------------------------------------
var savedDraft =graphClient.Users[objectID].Messages.PostAsync(draftMessage).GetAwaiter().GetResult();
//---------------------------------------------------Big Attachments-------------------------------------------------------
var fileStream = System.IO.File.OpenRead(bigAttachment.FullName);
var uploadRequestBody = new Microsoft.Graph.Users.Item.Messages.Item.Attachments.CreateUploadSession.CreateUploadSessionPostRequestBody
{
AttachmentItem = new AttachmentItem
{
AttachmentType = AttachmentType.File,
Name = bigAttachment.Name,
Size = fileStream.Length,
ContentType = "application/octet-stream"
}
};
var uploadSession =graphClient.Users[objectID]
.Messages[savedDraft.Id]
.Attachments
.CreateUploadSession
.PostAsync(uploadRequestBody).GetAwaiter().GetResult();
// Max slice size must be a multiple of 320 KiB
int maxSliceSize = 320 * 1024;
var fileUploadTask = new LargeFileUploadTask(uploadSession, fileStream, maxSliceSize);
var totalLength = fileStream.Length;
// Create a callback that is invoked after each slice is uploaded
IProgress progress = new Progress(prog =>
{
_Msg = $"Uploaded {prog} bytes of {totalLength} bytes";
});
try
{
// Upload the file
var uploadResult = fileUploadTask.UploadAsync(progress).GetAwaiter().GetResult();
_Msg = (uploadResult.UploadSucceeded ? "Upload complete" : "Upload failed");
}
catch (ServiceException ex)
{
_Msg = ($"Error uploading: {ex.ToString()}");
}
//--------------------------------------------------------Big Attachments Ends---------------------------------------------
//send the Draft
graphClient.Users[objectID].Messages[savedDraft.Id].Send.PostAsync().GetAwaiter().GetResult();
return _Msg + " send: succeed";
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... r-ms-graph