Learner
Learner

Reputation: 4004

Is it safe to call SmtpClient.Dispose() in .NET?

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.

Right now all 100 emails are sharing same SMTP session.

I was thinking that calling SmtpClient.Dispose() will take care of what I need. Please correct me if I am wrong.

So, basically 3 questions:

  1. Will SmtpClient.Dispose() take care of what I need?
  2. If Yes, is it safe to Dispose() SmtpClient without affecting other services on the server?
  3. If No, What would be the right approach to achieve what I want?

Sample 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]:[" & ex.Message.ToString & "] " & vbCrLf & "To:" & ToEmailList & vbCrLf & "From:" & FromEmail & vbCrLf & "Subject: " & Subject & vbCrLf & "Body: " & body)
    End Try
    message.Dispose()
End Sub

And this is how the method is being executed:

 For Each Item In ItemListCollection
 m_MailServer = New SmtpClient(MailServerName)
 MailServer.Credentials = New System.Net.NetworkCredential(MailServerUserName, MailServerPassword)
 SendMail(WeeklyMailServer, msgBody, msgSubject, MsgFromEmail, "[email protected]", rptAttachment)
 Next

Upvotes: 2

Views: 1507

Answers (2)

xpda
xpda

Reputation: 15813

Inside execution loop, you can enclose the code in a Using block. This will use a separate smtpclient for each email and will dispose / close it properly.

 For Each Item In ItemListCollection
   using m_MailServer as New SmtpClient(MailServerName)
     MailServer.Credentials = New System.Net.NetworkCredential(MailServerUserName, MailServerPassword)
     SendMail(WeeklyMailServer, msgBody, msgSubject, MsgFromEmail, "[email protected]", rptAttachment)
   end using
   Next

Upvotes: 1

scartag
scartag

Reputation: 17680

You could wrap it in a using statement and ensure that it is disposed when execution leaves the block. And you can call Send multiple times in a loop using the same SmtpClient.

Using client = New SmtpClient()

    For i As Integer = 0 To 99
        Dim message = New MailMessage()
        'initialization of whatever is needed
        ' message creation

        client.Send(message)

    Next
End Using

Upvotes: 3

Related Questions