0

I am working on a PowerShell script that needs to forward emails that I get with the Graph API. I already have the Azure App Registration and necessary consents figured out and can get the emails via Get-MGuserMessage. The issue is when I call Invoke-MgForwardUserMessage that the script hangs for about 30 seconds then fails with: tooManyRetries ErrorCode. I am calling the function with the below code. Most of what is written below is some form of what the Microsoft docs say to write... Any ideas?

$userId is my ObjectID in Azure. $mail.Id is the GraphAPI gotten message "Id" field.

$params = @{
            Message = @{
                IsDeliveryReceiptRequested = $true
                ToRecipients = @(
                    @{
                        EmailAddress = @{
                            Name    = "Not Real Person"
                            Address = "notreal@notreal.org"
                        }
                    }
                )
            }
            Comment = "Just want to make sure you get this."
        }

    Invoke-MgForwardUserMessage -UserId $userId -MessageId $mail.Id -BodyParameter $params
  • Do you get the same issue in the [Graph Explorer?](https://developer.microsoft.com/graph/graph-explorer) Worth checking and reporting to Microsoft if it does work via Graph Explorer. If it doesn't work there, please edit your question and add the list of permissions on the App Registration. – Ash Mar 23 '22 at 21:14

1 Answers1

0

When the user sends too many requests within a short span of time, the 429 error occurs and if the user exceeds default service limits this error occurs.

Limit requests for resource (email) is 1 request per 15 minutes and 3 requests per 24 hours.

For email, the resource is a unique network message ID/recipient pair.

Initially try the Invoke-MgForwardUserMessage command in graph explorer and see if works and check all permissions to be granted for graph api to operate on email and then try in PowerShell.

To handle the throttling,

  • Try reducing the number of operations per request.
  • Try reducing the frequency of calls.
  • Try avoiding immediate retries, because all requests against your usage limits.

Reference: Best practices to handle the throttling.

To resolve the issue, please try the below,

  • If you know the error code and message, you can try to handle it after some time by using sleep.
  • To detect throttling, use the HTTP error code 429 or 501 0r 503.
  • Check the number of seconds specified in the Retry-After response header and retry the request.Continue till it succeeds.

If still the issue exists, please check by including this sample code.

References : Ref1 , Ref2 , Ref3.

Rukmini
  • 6,015
  • 2
  • 4
  • 14