I followed the code to send the email with small and large attachment. It got the time out when I have a large attachment. I found that the code Progress object is functioning, ” IProgress progress = new Progress(prog =>” didn’t run. The attached file is 4,882 kb. If the email only has small file and send email. Is my code missing something or server require some setting? Hope someone tell me how to fix it. Thanks in advance.
I Am Having Trouble with Large Attachments When Sending Email in Microsoft Graph Library 5 Using .NET 4.7.2
The code snippet of the link:
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<Recipient>() { new Recipient() { EmailAddress = new EmailAddress() { Address = recipient } } };
//---------------------------------------------------Little Attachments-------------------------------------------------------
byte[] littleStream = File.ReadAllBytes(littleAttachment.FullName);
draftMessage.Attachments = new List<Microsoft.Graph.Models.Attachment>
{
new Microsoft.Graph.Models.Attachment
{
OdataType = "#microsoft.graph.fileAttachment",
Name = littleAttachment.Name,
AdditionalData = new Dictionary<string, object>
{
{
"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<FileAttachment>(uploadSession, fileStream, maxSliceSize);
var totalLength = fileStream.Length;
// Create a callback that is invoked after each slice is uploaded
IProgress<long> progress = new Progress<long>(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";
}