Reputation: 11
I use MS Graph API to send an email. I can send email with a small size attachment but I got an error when sending a large attachment:
Before progress after progressGeneral error uploading testFile.pdf: An error occurred while sending the request.
Inner exception: Error 12002 calling WINHTTP_CALLBACK_STATUS_REQUEST_ERROR, 'The operation timed out'. Email sent successfully
I set string to capture "Uploaded {prog} bytes of {totalLength} bytes" but it has nothing. I guess it may be upload issue.
Would you be able to give me a suggestion or example to solve this issue? Thanks in advance.
This is my code to create GraphServiceClient
:
GraphServiceClient myGPClient = new GraphServiceClient(new ClientSecretCredential(tenantId, clientId, secretVal));
There is my code to attach the large attachment:
try
{
string fileName = Path.GetFileName(largeAttachment);
string contentType = GetMimeType(largeAttachment);
// Add a small delay between attachment uploads
await Task.Delay(1000);
using (var fileStream = new FileStream(largeAttachment, FileMode.Open, FileAccess.Read))
{
var uploadbody = new Microsoft.Graph.Users.Item.Messages.Item.Attachments.CreateUploadSession.CreateUploadSessionPostRequestBody
{
AttachmentItem = new AttachmentItem
{
AttachmentType = AttachmentType.File,
Name = fileName,
Size = fileStream.Length,
ContentType = contentType
}
};
var uploadSession = await myGPClient.Users[emailUser]
.Messages[savedDraft.Id]
.Attachments
.CreateUploadSession
.PostAsync(uploadbody);
if (uploadSession == null || string.IsNullOrEmpty(uploadSession.UploadUrl))
{
strErrorMsg += $"Failed to create upload session for {fileName}. Upload URL is null.{Environment.NewLine}";
continue;
}
// Max slice size must be a multiple of 320 KiB
int maxSliceSize = 320 * 1024 * 9;
var fileUploadTask = new LargeFileUploadTask<FileAttachment>(uploadSession, fileStream, maxSliceSize);
var totalLength = fileStream.Length;
strErrorMsg += System.Environment.NewLine + "before progress";
// Create a callback that is invoked after each slice is uploaded
IProgress<long> progress = new Progress<long>(prog =>
{
//didn't have totalLength value on strErrorMsg
strErrorMsg = strErrorMsg + System.Environment.NewLine + $"Uploaded {prog} bytes of {totalLength} bytes";
});
strErrorMsg += System.Environment.NewLine+ "after progress";
// Upload the file
var uploadResult = await fileUploadTask.UploadAsync(progress);
if (!uploadResult.UploadSucceeded)
{
strErrorMsg = strErrorMsg + System.Environment.NewLine + $"Upload failed for file: {largeAttachment}";
}
else
{
strErrorMsg = strErrorMsg + System.Environment.NewLine + $"Successfully uploaded: {Path.GetFileName(largeAttachment)}";
}
}
}
catch (ServiceException ex)
{
strErrorMsg += $"Service error uploading {Path.GetFileName(largeAttachment)}: {ex.Message}{Environment.NewLine}";
if (ex.InnerException != null)
{
strErrorMsg += $"Inner exception: {ex.InnerException.Message}{Environment.NewLine}";
}
// Continue with the next attachment despite the error
continue;
}
catch (Exception ex)
{
strErrorMsg += $"General error uploading {Path.GetFileName(largeAttachment)}: {ex.Message}{Environment.NewLine}";
if (ex.InnerException != null)
{
strErrorMsg += $"Inner exception: {ex.InnerException.Message}{Environment.NewLine}";
}
// Continue with the next attachment despite the error
continue;
}
try
{
// Send the draft
await myGPClient.Users[emailUser].Messages[savedDraft.Id].Send.PostAsync();
strErrorMsg += "Email sent successfully";
}
catch (ServiceException ex)
{
strErrorMsg += $"Error sending email: {ex.Message}{Environment.NewLine}";
}
Upvotes: 0
Views: 22