Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am implementing some multiple email sending application with Csharp.

Basically I am just sending 100 emails in three different threads with indepent smtp parameters. Emails can be sent at the same time in the three different threads.

This simple code works for now in Sandbox application.

However, many articles mentioned that sending emails is I/O bounded process.

I am afraid that one thread can cause the other thread failure while one smtp is connected to the port 25.

Fortunaltey, so far, just using this simple multiple threads model does not catch the I/O exception like this while sending 100 emails.

.net - Intermittent System.IO.IOException while using SmtpClient - Stack Overflow[^]


However, it is only sand box application and this code might carry serious problem in real world application which is not verified at the moment.

Does anyone recommend using Thread​Pool.​Queue​User​Work​Item Method or seeing value of using Thread​Pool.​Queue​User​Work​Item Method in this application to be absoulte safe ?


ThreadPool.QueueUserWorkItem Method (System.Threading) | Microsoft Docs[^]

Or is it sufficiently safe to use this simple threads model ?

Kind regards.

What I have tried:

Thread td1 = new Thread(SendManyEmails);

Thread td2 = new Thread(SendManyEmails);

Thread td3 = new Thread(SendManyEmails);
Posted
Updated 15-Mar-21 4:04am

1 solution

Take a look at System.Threading.Channels or the older System.Threading.Tasks.Dataflow libraries. Both allow you to create fan out the number of tasks that pull from concurrent queues and act on it.

We use it for a number of things, including sending millions of newsletters each day.
 
Share this answer
 
v3
Comments
auto9817 15-Mar-21 10:13am    
I can tell that is in the nuget package. So you are saying both simple thread model and threadpool mode is unsafe potentially ?
Matthew Dennis 15-Mar-21 11:25am    
No, its just so easy to do it right with the packages.
auto9817 15-Mar-21 12:34pm    
Yes, I agree. But I want to learn them by coding by myself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900