How to best manage SMTP clients

676 views Asked by At

The MSDN docs for SmptClient say that instance members are not guaranteed to be thread-safe (born out by the use of instance properties to store things like the MailWriter in the reference source).

On the other hand, this post (and my experience) suggests that simply creating and disposing an SmtpClient for each email sent can cause you to hit the connection limit pretty easily.

What's the best way to manage these objects? Do I have to manually create a pool or throttle usage with a semaphore? Or, is there some easier pattern to follow. I'm always using the same mail server.

3

There are 3 answers

0
Ruskin On

The posts are indeed on the right track, created a SmtpClient object for every email you send is not the right approach. What I have done is setup a Queue. I have then started a thread which listens for any messages on the queue and sends them off using the same SmtpClient instance. The pseudo-code would be:

while (queue.HasEmails)
{
    SendEmail()
}
WaitForAFlag // So we don't keep spinning

Everytime an item is added to the queue you simply SetTheFlag so the thread will start reading the queue again.

0
AL-Tamimi On

I would recommend using a persistent MessageQueue such as RabbitMQ to send an event along with a payload to that queue. I will write a consumer to read off that queue, and send it to an email service that will handle the sending, logging, and raising additional email events that you may want to track for BI reasons.

That being said, if you are sending in thousands of emails per day, you will need to certify your IP address for sending such load. This process takes a long time, but once that IP is certified, you should experience no issues.

2
dmarietta On

Don't reinvent the tools that you already have available. Look at the DeliveryMethod property for the SmtpClient class. One of the options is to have the messages automatically queue up for you and delivered in the background via the Microsoft SMTP Service. Set the DeliveryMethod to PickupDirectoryFromIis and the messages will be written to the queue mail folder as fast as you can generate them. Then all you have to do is configure the Microsoft SMTP Server to forward the outgoing mail to a local mail server and you are done. If you look for references to the older CDOSYS methods using the mail pickup option, this works exactly the same way.