I’m trying to remove attachments from a draft email when forwarding it using the Microsoft Graph API in a C# application. However, I’m encountering a “tooManyRetries” error when trying to delete the attachments.
Here is the relevant code:
private static async Task<Message> GetDraftMessageAsync(GraphServiceClient client, EmailCreateServiceModel model,
ComposeMode mode, CancellationToken cancellationToken)
{
//other modes
var message = await client.Users[model.OriginalEmailAddress]
.Messages[model.MessageId]
.CreateForward()
.Request()
.Header("Prefer", "IdType="ImmutableId"")
.PostAsync(cancellationToken)
.ConfigureAwait(false);
// Question 1: can I somehow get the draft message with attachments without this second call?
var messageWithAttachments = await client.Users[model.OriginalEmailAddress]
.Messages[message.Id]
.Request()
.Expand(m => m.Attachments)
.GetAsync(cancellationToken)
.ConfigureAwait(false);
return messageWithAttachments;
}
public static async Task<Message> SendEmailAsync(EmailCreateServiceModel model, ComposeMode mode, List<string> keepingAttachments,
CancellationToken cancellationToken)
{
var client = await AuthService.GetGraphClientAsync();
var draftMessage = await GetDraftMessageAsync(client, model, mode, cancellationToken)
.ConfigureAwait(false);
if (mode is ComposeMode.Forward && draftMessage.Attachments != null)
{
var attachmentsToRemove = draftMessage.Attachments
.Where(attachment => !keepingAttachments.Contains(attachment.Name))
.ToList();
foreach (var attachment in attachmentsToRemove)
{
// Question 2: This code hangs for a long time and then throws the exception
await client.Users[model.OriginalEmailAddress]
.Messages[draftMessage.Id]
.Attachments[attachment.Id]
.Request()
.DeleteAsync(cancellationToken)
.ConfigureAwait(false);
}
}
draftMessage.From = string.IsNullOrEmpty(model.From)
? null
: new() { EmailAddress = new() { Address = model.From } };
draftMessage.ToRecipients = GetRecipients(model.To);
draftMessage.CcRecipients = GetRecipients(model.Cc);
draftMessage.BccRecipients = GetRecipients(model.Bcc);
draftMessage.Subject = model.Subject;
draftMessage.Body = new ItemBody() { Content = model.Body, ContentType = BodyType.Html, };
draftMessage = await client.Users[model.OriginalEmailAddress]
.Messages[draftMessage.Id]
.Request()
.UpdateAsync(draftMessage, cancellationToken)
.ConfigureAwait(false);
}
Problem:
- Can I somehow get the draft message with attachments without making
a second call? - The code (from the part shown) hangs for a long time and then throws an
exception when trying to delete attachments from the draft message.
Error Details:
Microsoft.Graph.ServiceException
HResult=0x80131500
Message=Code: tooManyRetries
Message: More than 3 retries encountered while sending the request.
Source=Microsoft.Graph.Core
StackTrace:
at Microsoft.Graph.HttpProvider.<SendRequestAsync>d__19.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
...
at Microsoft.Graph.AttachmentRequest.<DeleteAsync>d__3.MoveNext() in AttachmentRequest.cs
...
Questions:
- Is there a specific permission required to delete attachments from a draft message?
- Is there an issue with how I’m fetching and modifying the draft message?
- How can I handle the “tooManyRetries” error properly?
Any help or guidance on resolving this issue would be greatly appreciated.