I'm trying to send any number of emails in the shortest amount of time using .NET 5.0?
I've been playing with something like the following but I am not sure if it is optimal or even correct as there are a number of elements I don't understand.
public async Task SendEmailAsync(string subject, string htmlMessage,
IEnumerable<string> recipients, string? attachment)
{
using SemaphoreSlim semaphore = new(10, 10);
await Task.WhenAll(recipients.Select(async recipient =>
{
await semaphore.WaitAsync();
try
{
return SendEmailAsync(subject, htmlMessage, recipient, attachment);
}
finally
{
semaphore.Release();
}
}));
}
Can someone clarify if this is correct, or let me know if they know a better approach?