title: Is it safe to call SmtpClient.Dispose() in .NET?
body: <p>I have a scenario where I need to send 100 emails in one shot (using a loop), but also I am not allowed to send 1 email per SMTP session.</p>
  
  <p>Right now all 100 emails are sharing same SMTP session.</p>
  
  <p>I was thinking that calling SmtpClient.Dispose() will take care of what I need. Please correct me if I am wrong.</p>
  
  <p>So, basically 3 questions:</p>
  
  <ol>
  <li><strong>Will SmtpClient.Dispose() take care of what I need?</strong> </li>
  <li><strong>If Yes, is it safe to Dispose() SmtpClient without affecting other services on the
  server?</strong> </li>
  <li><strong>If No, What would be the right approach to achieve what I
  want?</strong></li>
  </ol>
  
  <p><strong>Sample Code:</strong></p>
  
  <pre><code>Private Shared Sub SendMail(ByVal MailServer As SmtpClient, ByVal body As String, ByVal Subject As String, ByVal FromEmail As String, _
                                 ByVal ToEmailList As String, Optional ByVal AttFile As Attachment = Nothing)
  
      Dim message As New MailMessage
  
      Try
          message.From = New MailAddress(FromEmail)
  
          message.Subject = Subject
          message.IsBodyHtml = False
          message.Body = body
          message.Priority = MailPriority.High
  
          If Not AttFile Is Nothing Then
              message.Attachments.Add(AttFile)
          Else
              message.Attachments.Add(AttFile)
          End If
  
          MailServer.Send(message)
      Catch ex As Exception
          Throw New ApplicationException("SERVICE1.SendMail ERROR -- Error sending email [ERROR]:[" &amp; ex.Message.ToString &amp; "] " &amp; vbCrLf &amp; "To:" &amp; ToEmailList &amp; vbCrLf &amp; "From:" &amp; FromEmail &amp; vbCrLf &amp; "Subject: " &amp; Subject &amp; vbCrLf &amp; "Body: " &amp; body)
      End Try
      message.Dispose()
  End Sub
  </code></pre>
  
  <p>And this is how the method is being executed:</p>
  
  <pre><code> For Each Item In ItemListCollection
   m_MailServer = New SmtpClient(MailServerName)
   MailServer.Credentials = New System.Net.NetworkCredential(MailServerUserName, MailServerPassword)
   SendMail(WeeklyMailServer, msgBody, msgSubject, MsgFromEmail, "xyz@abc.com", rptAttachment)
   Next
  </code></pre>