Search code examples
c#sendgridsmtpclientparallel.foreach

Problem whit client.SendEmailAsync and parallel method


I have a problem with sendEmailAsync and Parallel.ForEach

If I don´t use Parallel.ForEach it works perfectly, but when I try to use it this way, my console application stops and I don´t receive any response. My question: is SendEmailAsync available for parallel method?

//here is my code
Parallel.ForEach(registros, async item => 
{
   //...
   var response = await client.SendEmailAsync(myMessage).ConfigureAwait(false);   
}

Solution

  • The SmtpClient instance can only handle one send at a time. To do what you want you’ll need a separate instance in each thread.

    But don’t overwhelm your SMTP server or it may figure you for a spammer and put you on a blocklist.