SmtpClient - What is proper lifetime?

后端 未结 3 1757
佛祖请我去吃肉
佛祖请我去吃肉 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:22

    First of all it is a very good practice to use any object whenever needed untill and unless you needs its utility throughout the app.

    Secondly you should create SmtpClient's object every time you need it and to properly dispose it, for that use the using tags as described above by Glitch100.

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

提交回复
热议问题