Inconsistent Upload Times and Timeout for Large Attachments using Microsoft Graph API

Diluka Hewage 56 Reputation points
2023-08-07T04:07:37.1066667+00:00

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?

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Md Asif Muztaba 325 Reputation points Microsoft External Staff
    2023-12-29T05:03:09.2666667+00:00

    The inconsistency in upload times and timeouts when uploading large attachments using the Microsoft Graph SDK could be due to several factors:

    1. Network Conditions: Variations in network conditions such as bandwidth, latency, or packet loss can significantly affect upload times.
    2. Server Load: The server handling your requests might be experiencing varying levels of load, which can impact response times.
    3. Throttling: Microsoft Graph has throttling limits, and if you’re hitting these limits, it could cause delays. Each application is limited to uploading 15 megabits every 30 seconds per mailbox.
    4. Attachment Size: The attachment sizes you’re dealing with are around 100-120 megabytes, which are quite large. The upload process works fine for smaller attachments, which suggests that the size of the attachments could be a factor.
    5. Upload Session: 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. If there’s an issue with the upload session, it could potentially cause this problem.

    As a next step, you could try to isolate the issue by testing the upload under different network conditions, with different server loads, or with different attachment sizes.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.