SmtpClient - What is proper lifetime?

后端 未结 3 1714
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-17 15:56

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. Batch

3条回答
  •  一整个雨季
    2021-01-17 16:23

    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.

提交回复
热议问题