Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SmtpClient - What is proper lifetime?

I'm creating Windows Service that sends batches of emails every 5 minutes.

I want to send batches of 10-100 emails every 5 minutes. This is extreme edge case. Batches are sent every 5 minutes and normally consist of up to 10 emails.

I'm using SmtpClient from System.Net.Mail namespace.

What is proper lifetime of SmtpClient object? Should I create one every time batch is send? Or should I create one on Service start and never dispose of it?

like image 802
Hooch Avatar asked Jun 02 '15 08:06

Hooch


2 Answers

You should always utilise using

using (var smtpClient = new SmtpClient())
{
    smtpClient.SendMail(message);
}

In most cases you should always dispose of anything that implements IDisposable as soon as you are finished with it, however you should always check the Docs to be sure. The SmtpClient class in .NET 4.0 implements IDisposable so be sure to use it!

To quote MSDN:

The SmtpClient class has no Finalize method, so an application must call Dispose to explicitly free up resources.

If you find yourself doing async related tasks then you can make a new instance for each email to prevent blocking yourself.You can use the following.

var smtpClient = new SmtpClient();
smtpClient.SendCompleted += (s, e) => {
                           client.Dispose();
                           message.Dispose();
                        };
client.SendAsync(message, null);

At Request - Best option for Bulk Send Emails

As noted above you can reuse the same client. If you keep it all on the same thread I recommend you just use one client

MSDN States:

The SmtpClient class implementation pools SMTP connections so that it can avoid the overhead of re-establishing a connection for every message to the same server. An application may re-use the same SmtpClient object to send many different emails to the same SMTP server and to many different SMTP servers.

However it goes on to say:

...As a result, there is no way to determine when an application is finished using the SmtpClient object and it should be cleaned up.

So assuming you dispose of your Client when complete it is fine.


There is discussion of a number of SMTP related topics linked below as I recently found myself asking the same question

More from Stackoverflow:

What are best practices for using SmtpClient, SendAsync and Dispose under .NET 4.0

How to dispose objects having asynchronous methods called?

Related Reading:

MSDN SmtpClient

Implementing Finalize and Dispose to clean up managed resources

like image 173
JonE Avatar answered Oct 04 '22 08:10

JonE


As of .NET 4.0 the SmtpClient pools connections so you might keep on it for a while. It's probably best to dispose it after you finished sending a batch.

From MSDN: https://msdn.microsoft.com/en/us/library/system.net.mail.smtpclient(v=VS.100).aspx

The SmtpClient class implementation pools SMTP connections so that it can avoid the overhead of re-establishing a connection for every message to the same server. An application may re-use the same SmtpClient object to send many different emails to the same SMTP server and to many different SMTP servers. As a result, there is no way to determine when an application is finished using the SmtpClient object and it should be cleaned up.

like image 36
rlee Avatar answered Oct 04 '22 09:10

rlee