Speed ​​up sending multiple emails through an SMTP server using System.Net.Mail

I am new to C #, but I learned a lot from VB.Net about programming in .NET for Windows.

I just made a simple SMTP client that sends emails from a program. This is a console application and can send only one email message through the server at a time. This is very slow, and I need to send several emails through my client at the same time.

Is this possible in C #?

+6
source share
1 answer

just use multiple threads (multiple processes).

In C #, you can do this with a task.

new Task(delegate { smtpClient.send(myMessage); }).Start(); 

Just wrap the send command in this object and it will be sent asynchronously.

Be careful if this is wrapped in a loop, it will start a new process for each mail.

if you need to send a large number of letters at the same time, I suggest you use ThreadPool . It allows you to control the number of matching threads that you would like to have at the same time.

+7
source

All Articles