I am facing a puzzling issue while using the Microsoft Graph SDK to upload large attachments to email messages in Outlook. The problem is related to the upload times, which seem to be inconsistent, and the occurrence of timeouts during the process.
I am using the Graph SDK to upload large attachments to email messages. The process involves initiating an upload session with the first API call and then using the returned upload session URL to upload the attachment in smaller parts.
The strange part is that when I perform the same action for the same email attachment, the upload time varies significantly. Sometimes, the upload completes within a reasonable time frame, around 2 minutes, but on other occasions, it takes more than 7 minutes, causing timeouts and ultimately failing the upload.
This is the code I am using to do the upload for large attachment and send email
public async Task SendEmailAsAttachmentAsync(Stream content, string mailboxAddress)
{
var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
Message = new Message
{
Subject = "{Subject}",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "{content}"
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "{recipient-address}",
},
},
},
},
};
//create draft message
var draftItem = await _graphServiceClient.Users[mailboxAddress].Messages.PostAsync(requestBody.Message);
//create an upload session
var uploadRequestBody = new Microsoft.Graph.Users.Item.Messages.Item.Attachments.CreateUploadSession.CreateUploadSessionPostRequestBody
{
AttachmentItem = new AttachmentItem
{
AttachmentType = AttachmentType.File,
Name = "attachment name.eml",
Size = content.Length
}
};
var uploadSession = await _graphServiceClient.Users[mailboxAddress].Messages[draftItem.Id].Attachments.CreateUploadSession.PostAsync(uploadRequestBody);
if (await UploadAttachmentContentAsync(content, uploadSession))
{
await _graphServiceClient.Users[mailboxAddress].Messages[draftItem.Id].Send.PostAsync();
}
}
private static async Task<bool> UploadAttachmentContentAsync(Stream content, UploadSession uploadSession)
{
// Max slice size must be a multiple of 320 KiB
int maxSliceSize = 320* 1024;
var fileUploadTask = new LargeFileUploadTask<Attachment>(uploadSession, content, maxSliceSize);
long uploadedBytes = 0;
// Create a callback that is invoked after each slice is uploaded
IProgress<long> progress = new Progress<long>(prog =>
{
uploadedBytes = uploadedBytes + prog;
});
try
{
// Upload the file
var uploadResult = await fileUploadTask.UploadAsync(progress);
if (uploadResult.UploadSucceeded)
{
return true;
}
return false;
}
catch (Exception ex)
{
....
}
}
Additional Information:
- I am using graph version 5.21.0
- The attachment sizes I'm dealing with is around 100-120 megabytes.
- The upload process works fine for smaller attachments.
- I obtained this information through Azure Application Insights using transaction search to monitor the API call performance.
What could be causing this inconsistency in upload times for the same email attachment?
Why does the same API call sometimes succeed within a few minutes and at other times, take much longer and eventually timeout?