I have a .Net5 application and I am trying to send emails using SmtpClient. As I understand it, I should wrap it in a Using statement
So I have the following
using(SmtpClient client = new SmtpClient(host, port))
{
....my code.....
client.SendAsync(message, userState);
}
However this succeeds but I never receive the email.
If I change the code to
SmtpClient client = new SmtpClient(host, port);
....my code.....
client.SendAsync(message, userState);
Then I do receive the email.
I take it that client is being disposed before the email has been sent correctly but then I thought "using" was supposed to handle that properly?
What is the best practice to dispose of client? Also I may need to send multiple emails in using the same code so I need to make sure the client new gets created each time so that I do not dispose of it while it is sending another email.