Since sending email is an I / O-bound process, therefore spawning streams for sending emails will not bring much success (if any) to speed up.
If you use an SMTP server in this part of Windows, then when you send an email, it is not actually sent at that moment. It is queued on the server, and the server sends them as quickly as possible. Sending email is actually a slow process.
I assume that I say there are two options:
- Just send them in sequence and see if this meets your performance requirements.
- " ". " " - #/. NET
, , ( ). , , , ( , ), ( I/O , /, /).
. , . , .
.
, .
ThreadPool .NET 4.0 ( Parallel.For PLINQ Tasks), , " ", .
Parallel.For/Parallel.ForEach , .
.NET 3.5. , , Parallel.For/ForEach. , ( ), ThreadPool Parallel Data.
private static void SendEmailsUsingThreadPool(List<Recipient> recipients)
{
var coreCount = Environment.ProcessorCount;
var itemCount = recipients.Count;
var batchSize = itemCount / coreCount;
var pending = coreCount;
using (var mre = new ManualResetEvent(false))
{
for (int batchCount = 0; batchCount < coreCount; batchCount++)
{
var lower = batchCount * batchSize;
var upper = (batchCount == coreCount - 1) ? itemCount : lower + batchSize;
ThreadPool.QueueUserWorkItem(st =>
{
for (int i = lower; i < upper; i++)
SendEmail(recipients[i]);
if (Interlocked.Decrement(ref pending) == 0)
mre.Set();
});
}
mre.WaitOne();
}
}
private static void SendEmail(Recipient recipient)
{
}
}
class Recipient
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
}
, SendEmailUsingThreadPool(), . , :). DataSet/DataTable, , DataSet/DataTable. , . .