SmtpClient - What is the correct lifespan?

I am creating a windows service that sends batches of emails every 5 minutes.

I want to send batches of 10-100 emails every 5 minutes. This is the extreme edge. Packages are sent every 5 minutes and usually consist of 10 letters.

I am using SmtpClient from the System.Net.Mail namespace.

What is the correct lifetime of the SmtpClient object? Do I have to create it every time a package is sent? Or should I create it at the start of the service and never delete it?

+3


source to share


3 answers


You should always use using

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

      

You should always dispose of anything that implements IDisposable as soon as you're done with it. The SmtpClient class in .NET 4.0 implements IDisposable, so be sure to use it!

To quote MSDN:

The SmtpClient class does not have a Finalize method, so the application must call Dispose to explicitly release resources.

If you find yourself doing asynchronous tasks, you can create 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);

      

In request - the best option for bulk mailings

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

MSDN status:

The SmtpClient class script bundles SMTP connections so that it can avoid the overhead of reestablishing a connection for each message to the same server. An application can reuse the same SmtpClient Object to send many different emails to the same SMTP server and many different SMTP servers.



However, he continues:

... As a result, it is not possible to determine when the application is using the SmtpClient object and should be cleaned up.

So, assuming that you dispose of your Client upon completion, that's okay.


There are a number of SMTP related topics discussed below as I recently found myself asking the same question

More from Stackoverflow:

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

How to delete objects with asynchronous methods?

Related reading:

MSDN SmtpClient

Implementing Finalize and Dispose to Clean Up Managed Resources

+5


source


As of .NET 4.0, SmtpClient connections are pooled, so you can continue with it for a while. It's probably best to dispose of it after you've finished sending the batch.

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



The SmtpClient class function aggregates SMTP connections to avoid the overhead of reconnecting for every message on the same server. An application can reuse the same SmtpClient object to send many different emails to the same SMTP server and to many different SMTP servers. As a result, it is not possible to determine when the application will terminate using the SmtpClient object and must be cleaned up.

+3


source


First of all, it is very useful to use any object when needed, until you need its utility throughout the application.

Second, you must create and dispose of the SmtpClient object every time you need it, for which you use use tags as described above, Glitch100.

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

      

0


source







All Articles