title: Best way to make send bulk emails parallel
body: <p>I am new to TPL (Task Parallel Library) and I'm having a hard time trying to configure my process to run tasks in parallel.</p>
  <p>I'm working on an application to send bulk emails (like thousands per minute, that's the idea), but when I see the processors performance, it's not good: <strong>I'm pretty sure there's a lot of overhead cause I'm not using the Task library properly</strong>.</p>
  <p>Here's my code:</p>
  <pre><code>public async void MainProcess()
  {
      var batches = emailsToProcess.Batch(CONST_BATCHES_SIZE);
      
      foreach (var batch in batches.AsParallel()
          .WithDegreeOfParallelism(Environment.ProcessorCount))
      {
           await Task.WhenAll(from emailToProcess in batch 
                      select ProcessSingleEmail(emailToProcess));
          _emailsToProcessRepository.MarkBatchAsProcessed(batch);
      }
  }
  
  private async Task ProcessSingleEmail(EmailToProcess emailToProcess)
  {
      try
      {
          MailMessage mail = GetMail(emailToProcess); //static light method
          await _smtpClient.SendAsync(sendGridMail);
          emailToProcess.Processed = true;
      }
      catch (Exception e)
      {
          _logger.Error(ErrorHelper.GetExceptionMessage(e, 
                      string.Format(&quot;Error sending Email ID #{0} : &quot;, 
                      emailToProcess.Id)), e);
      }
  }
  </code></pre>
  <p><em>(I know it might look awful: please feel free to roast me ☺)</em></p>
  <p>I need it to behave this way: I need to process a number of records in a batch (btw, I'm using a library that allows me to use the &quot;Batch&quot; method), cause I need to mark a batch of records as processed in the database as the process completes sending them.</p>
  <p>The process is actually doing what I want: except is <strong>slow as hell</strong>. And as you can see in the perfmon, the processors are not working at a very high capacity:</p>
  <p><img src="https://i.stack.imgur.com/dQ7Y2.png" alt="enter image description here" /></p>
  <p>What's the best way to do this? Any advice?</p>
  <p><strong>EDIT:</strong> I realize that what I have is an overhead problem.
  Is there any tool or easy way to detect and correct them?</p>