Reputation: 586
I want to send a newsletter to our site's registered users - more than 100000
Today, I send the mails in a simple for
loop, which is very heavy and slow.
I want it to finish the process as fast as possible (even if the process would run in the background and didn't bother the users' UI)
I tried TPL
, Async
, smtpClient.SendAsyncMail()
and more, but never could actually create a working example that proves that these systems work faster (or work at all).
I've read many posts and explanations using thread, but they seem to be old, deprecated or irrelevant, since there are new technologies I surely don't know of.
Could you please tell me what is the most efficient and fastest way to send multiple emails in parallel? Could you show me an example that works?
UPDATE 20/06/2018
Each email is different for each user. Therefore a BCC solution is irrelevant for me.
I will specify my question a little more:
I only focus on the high-level where I use a sending function, such as ASP.NET smtpClient.Send()
to send the mails to the SMTP.
I ignore for now the part of the SMTP itself. I have a great SMTP server that knows how to handle the queuing.
The main pitfall right now is the Send function that uploads the bytes to the SMTP server in ASP.NET. That's where I couldn't find a way to send faster. (It is needles to say that if I buy a stronger server, even an iterative method will work faster)
But I want a scalable solution - a smarter one, where I can send in parallel huge amounts of mails.
UPDATE 28/06/2018
Apparently a parallel solution will not solve my problem. As explained by @Terry Carmen and others, I may have to look at other bottle necks in the process. Thank you all for helping! What a great community!!!
Upvotes: 0
Views: 575
Reputation: 3886
Add the users as BCC recipients to the message and let the mail server do the work.
The mail server probably has a BCC limit, so you may have to see what the server's limit is and batch the requests.
edit
Regarding "sending in parallel"
You're looking for the wrong answer. If your machine takes X seconds to produce Y emails, running them "in parallel" won't fix anything . The most you would do is split the generation between several cores, which might improve performance a little, but is almost certainly not the problem.
You need to see "what the actual slow part is". It might be your disk or CPU or network connection, or database server or possibly a rate limit setting on the mail server. The actual SMTPClient object is almost certainly not the issue.
another edit
You can try sending in parallel by running separate instances of SMTPClient in separate threads; However this is unlikely to actually help you.
As mentioned before by myself and others, you are assuming that parallel send is some sort of magic bullet. It's not. There is something that is rate-limiting you and it's almost certainly not that you're single threaded.
Upvotes: 1