Reputation: 429
This question is about SmtpClient in .NET 3.5. I am aware of the dispose change in .NET 4, but migrating isn't an option at the moment.
I'm wondering what happens with an smtpclient if you use sendasync and the smtpclient goes out of scope. Example:
public void SendSomething(){
SmtpClient smtp = new SmtpClient(...);
smtp.SendCompleted += SendCompletedCallback;
smtp.SendAsync(...);
}
private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e){
...
}
What happens if you send something like this, the object goes out of scope in the method and the smtpclient is a bit slow and only starts the actual sending now: does this give any problems? Or does the smtpclient protect itself from being garbage collected etc?
According to the msdn documentation you can't do a sendasync while another sendasync isn't finished yet. But what happens if you create two smtpclients and do a sendasync at the same time (for example two threads call the SendSomething() method at the same time). Can it handle this or will it create problems?
Another question: Currently in .NET 3.5 the smtp client doesn't send the quit command after it's done. This is fixed in .NET 4 with dispose. However how bad is this bug: does it cause problems for the smtp servers if the quit command isn't sent? Or is this something they should be able to handle?
Upvotes: 3
Views: 1067
Reputation: 887195
As long as the async operation is executing, the SmtpClient
is referencced by a method frame or callback delegate.
Don't worry about it.
Multiple SmtpClient
s will not interfere with eachother.
Upvotes: 2