Search code examples
c#multithreadingsocketssmtpbackgroundworker

asynchronously send emails via smtp servers - multithreadingsend emails


I have created a simple form which is loading a csv file with few columns like email, name, city and I'm trying to send them daily updates via 2 smtp servers I have. I thought to use a backgroundworker because it's progress capabilities(for the progressbar I have) but I read also on this website that there are other alternatives like task, thread or threadpool.

I also read that sending emails must be done with an async method. I don't know if it's possible or if it's optimized for performance, but I'm trying to do the following:

for each smtp I wanna use a thread to read, let's say, 10 lines from the csv file, split the line by fields and send the info to another thread which will send the message to each email in that small list. I wanna do this to save some precious time for each smtp auth procedure.

Because connecting to the smtp for each email requires the whole socket procedure everytime, like HELO, AUTH, DATA. If I'm gonna send a list of email, I can keep the socket open after auth and change only the data I send. 10 lines is just a variable which can be custom changed. So, 2 tasks for each smtp, means 4 in total or can be increased based on the number of smtps I will use.

Can I also use additional threads for each task or backgroundworker?

I'm kinda confused because I'm new to c# and I haven't found any example about this. It's been more than a week since I'm trying to understand how backgroundworker/threads work but still unsuccessfully. Any help would be appreciated and any idea better than mine to improve the performance Thanks!


Solution

  • First off you need to understand the difference between the different types of threads.

    A threadpool is just a collection of threads available for use. The threads used by the pool are managed by the pool & returned there after use. The idea is you get performance benefits because the cost of creating the thread only happens once.

    A background worker is a thread that runs in the background. The only difference between a background and foreground thread, is a background thread will not prevent a process from terminating.

    Sending emails async is more SHOULD rather than MUST. The smtp component has no concept of seperating the connect form the send.

    I think what you want to do is:

    • initalise a number of threads, say 10.
    • Read a line from the csv.
    • Pass this line to a thread from above. If no threads are available, wait till one is.
    • In the thread parse the line and send the mail. Sending doesn't need to be async with this method but you can if you want. When you're done sending, return the thread to the pool ready for the next line

    You can manage your threads as a pool - there are many examples around if you google for thread pools - or you can do it by hand using manualresetevents to handle the wait. The advantage of doing it with a pool is you can change the number of workers available easily without affecting your code.