Quickstart: Throw an exception when email sending tier limit is reached
In this quick start, you'll learn about how to throw an exception when the email sending tier limit is reached using our Email SDKs.
Throw an exception when email sending tier limit is reached
The Email API has throttling with limitations on the number of email messages that you can send. Email sending has limits applied per minute and per hour as mentioned in API Throttling and Timeouts. When you've reached these limits, subsequent email sends with SendAsync
calls receive an error response of “429: Too Many Requests”. By default, the SDK is configured to retry these requests after waiting a certain period of time. We recommend you set up logging with the Azure SDK to capture these response codes.
Alternatively, you can manually define a custom policy:
using Azure.Core.Pipeline;
public class Catch429Policy : HttpPipelineSynchronousPolicy
{
public override void OnReceivedResponse(HttpMessage message)
{
if (message.Response.Status == 429)
{
throw new Exception(message.Response);
}
else
{
base.OnReceivedResponse(message);
}
}
}
Add this policy to your email client to ensure that 429 response codes throw an exception rather than being retried.
EmailClientOptions emailClientOptions = new EmailClientOptions();
emailClientOptions.AddPolicy(new Catch429Policy(), HttpPipelinePosition.PerRetry);
EmailClient emailClient = new EmailClient(connectionString, emailClientOptions);
Throw an exception when email sending tier limit is reached
The Email API has throttling with limitations on the number of email messages that you can send. Email sending has limits applied per minute and per hour as mentioned in API Throttling and Timeouts. When you've reached these limits, subsequent email sends with send
calls receive an error response of “429: Too Many Requests”. By default, the SDK is configured to retry these requests after waiting a certain period of time. We recommend you set up logging with the Azure SDK to capture these response codes.
There are per minute and per hour limits to the amount of emails you can send using the Azure Communication Email Service. When you've reached these limits, any further beginSend
calls receive a 429: Too Many Requests
response. By default, the SDK is configured to retry these requests after waiting a certain period of time. We recommend you set up logging with the Azure SDK to capture these response codes.
Alternatively, you can manually define a custom policy:
const catch429Policy = {
name: "catch429Policy",
async sendRequest(request, next) {
const response = await next(request);
if (response.status === 429) {
throw new Error(response);
}
return response;
}
};
Add this policy to your email client to ensure that 429 response codes throw an exception rather than being retried.
const clientOptions = {
additionalPolicies: [
{
policy: catch429Policy,
position: "perRetry"
}
]
}
const emailClient = new EmailClient(connectionString, clientOptions);
Throw an exception when email sending tier limit is reached
The Email API has throttling with limitations on the number of email messages that you can send. Email sending has limits applied per minute and per hour as mentioned in API Throttling and Timeouts. When you've reached these limits, subsequent email sends with beginSend
calls receive an error response of “429: Too Many Requests”. By default, the SDK is configured to retry these requests after waiting a certain period of time. We recommend you set up logging with the Azure SDK to capture these response codes.
Alternatively, you can manually define a custom policy:
import com.azure.core.http.HttpResponse;
import com.azure.core.http.policy.ExponentialBackoff;
public class CustomStrategy extends ExponentialBackoff {
@Override
public boolean shouldRetry(HttpResponse httpResponse) {
int code = httpResponse.getStatusCode();
if (code == HTTP_STATUS_TOO_MANY_REQUESTS) {
throw new RuntimeException(httpResponse);
}
else {
return super.shouldRetry(httpResponse);
}
}
}
Add this retry policy to your email client to ensure that 429 response codes throw an exception rather than being retried.
import com.azure.core.http.policy.RetryPolicy;
EmailClient emailClient = new EmailClientBuilder()
.connectionString(connectionString)
.retryPolicy(new RetryPolicy(new CustomStrategy()))
.buildClient();
Throw an exception when email sending tier limit is reached
The Email API has throttling with limitations on the number of email messages that you can send. Email sending has limits applied per minute and per hour as mentioned in API Throttling and Timeouts. When you've reached these limits, subsequent email sends with SendAsync
calls receive an error response of “429: Too Many Requests”. By default, the SDK is configured to retry these requests after waiting a certain period of time. We recommend you set up logging with the Azure SDK to capture these response codes.
Alternatively, you can manually define a custom policy to ensure that 429 response codes throw an exception rather than being retried.
def callback(response):
if response.http_response.status_code == 429:
raise Exception(response.http_response)
email_client = EmailClient.from_connection_string(<connection_string>, raw_response_hook=callback)
Troubleshooting
Email Delivery
To troubleshoot issues related to email delivery, you can get status of the email delivery to capture delivery details.
Important
The success result returned by polling for the status of the send operation only validates the fact that the email has successfully been sent out for delivery. To get additional information about the status of the delivery on the recipient end, you will need to reference how to handle email events.
Email Throttling
If you see that your application is hanging it could be due to email sending being throttled. You can handle this through logging or by implementing a custom policy.
Note
This sandbox setup is to help developers start building the application. You can gradually request to increase the sending volume once the application is ready to go live. Submit a support request to raise your desired sending limit if you require sending a volume of messages exceeding the rate limits.
Clean up Azure Communication Service resources
If you want to clean up and remove a Communication Services subscription, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it. Learn more about cleaning up resources.
Next steps
In this quick start, you learned how to manually poll for status when sending email using Azure Communication Services.
You may also want to:
- Learn how to send email to multiple recipients
- Learn more about sending email with attachments
- Familiarize yourself with email client library