tolsen64
tolsen64

Reputation: 999

SmtpClient - Does calling dispose close only the connection it created? Or all connections as Microsoft seems to indicate?

A little background. Our ASP.NET website sends emails when forms are submitted. The current code looks like this:

Sub SendEmail(ByVal ToList As String, ByVal Subject As String, ByVal HTMLBody As String)
    Dim Message As New Mail.MailMessage("[email protected]", ToList)
    Message.Subject = Subject
    Message.Body = HTMLBody
    Message.IsBodyHtml = True

    Dim smtp As New SmtpClient("smtp.servername.com", 25)
    smtp.Send(Message)
End Sub

Intermittently, it gets the following response from the SMTP server:

Service not available, closing transmission channel. The server response was: 4.3.2 Service not active

Doing some research on that error, leads me to believe it's because the SMTP server doesn't like you for some reason. So I started looking at configuring the SmtpClient to see if it was correct in the code. What I found was this from a Microsoft web site:

The SmtpClient class has no Finalize method, so an application must call Dispose to explicitly free up resources. The Dispose method iterates through all established connections to the SMTP server specified in the Host property and sends a QUIT message followed by gracefully ending the TCP connection.

So I thought maybe too many connections left open during heavy usage of the page? Maybe I should call the Dispose method, or encapsulate in a Using block. But the phrase that stuck out at me was this one:

all established connections to the SMTP server

It doesn't say anything about the same process, page instance, or whatever. Just that ALL established connections will be terminated.

So what I want to know is, if person1 and person2 are submitting a page at the same time, could calling the dispose method from person1 potentially close the SMTP connection for person2 in the midst of it sending email?

Upvotes: 0

Views: 1638

Answers (1)

wannadream
wannadream

Reputation: 1760

From doc https://msdn.microsoft.com/en-us/library/ee706941(v=vs.110).aspx:

Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, and releases all resources used by the current instance of the SmtpClient class.

person1 and person2 will have its own instance so that they will not be affected by each other.

Upvotes: 1

Related Questions