EMAIL-Parallel sending or not?

Single Sign On (SSO)
May 23, 2016
10 Print- Part one
July 6, 2016

In my work I had a problem with sending large number of emails through application. I had an idea of parallel sending, because I thought: it is not good for application to be on hold while one mail is processed, and sending through SMTP client is time consuming process – it will take at least few seconds.
I found a solution which you can use to send large number of emails, and application will not be frozen.
Our class MailSender will look something like this. It will have some private members:

Also, this method is meant to be started in constructor:

This method will create as much clients as we put in appconfig.
My suggestion is to use several different email accounts. Why? Because if we use for e.g. gmail, in that case, we have limit of 100 emails per day with one account. If we have 5 accounts, than we can send 500 emails per day. Also, there exists size limit per day for each account. In my case I use same password for all emails.

Next method is for parallel sending emails, but be aware that it is not optimized, it is getting random SMTP client.

This is main method for parallel sending emails:

Disposing clients, of course..

And finally, method that sends mails.

As you can see, this method will not call next SMTP client, but first one that is available. That would be ok, if we would not have sending limits. Also, in this case, we must prepare all emails, with all attachments, and I am not sure how memory it will take. That’s why I am not using parallel mailer, but sending one-by-one.

In this case, sending emails will be one by one, but SMTP clients will be taken by order. Every time different SMTP client will be used: 1,2,3,4,5,1,2,3….

If you have smaller numer of emails, with known number and size, maybe parallel sending is correct way to do it. But if not, you may consider sending one-by-one.

Vladimir Starčević
Senior Developer