11

I have recently used Mailkit lib in our project in order replacing .NET SmtpClient. We have 2 business cases to use the SmtpClient to send emails. In one instance we use SmtpClient to send queued emails in a separate process and other instance we send emails instantly. While implementing I noticed that we have to call the Disconnect method of the Client instance. mailClient.Disconnect(...); It was not sure and not clear in the documentation what is the best way to call this method. So my question, What is the best practice to use this method? Call mailClient.Disconnect(true) per every message or mailClient.Disconnect(false)?

Out of interest, if I use the client within a using block, should I require to call Disconnect(...) explicitly after sending a message? I reckon it calls disconnect implicitly when the Dispose() gets executed. using (var mailClient = new SmtpClient()) { mailClient.Connect(...); mailClient.AuthenticationMechanisms.Remove("XOAUTH2"); mailClient.Authenticate(...); mailClient.Send(message); mailClient.Disconnect(false); }

Appreciate your feedback in this regard.

RSF
  • 510
  • 6
  • 18

1 Answers1

12

The Dispose() method will only close the socket connection if it is still alive (which is effectively the same as calling Disconnect (false)).

Calling Disconnect (true) is much more courteous, though, as it sends the appropriate LOGOUT or QUIT command to the server which allows the server to properly dispose of their resources.

jstedfast
  • 35,744
  • 5
  • 97
  • 110