I have a requirement where I need to convert .NET 4.0 project to .NET 3.5 project,
Everything else is fine except the “SmtpClient”
So far, I have found that .NET 3.5 SmtpClient does not implement IDisposable, while in .NET 4.0 it does!
Below is the code which works fine on .NET4.0 but not on .NET3.5:
Using MailServer As New SmtpClient(MailServerName)
MailServer.Credentials = New System.Net.NetworkCredential(MailServerUserName, MailServerPassword)
SendMail(MailServer, msgBody, msgSubject, FromEmail, ToEmail)
End Using
Any idea how to do it using .NET 3.5 (I prefer to use “Using” code block to auto dispose the object & not the old style manual dispose)
How about a
TryCasttoIDisposablebefore theUsing:If running in .NET 4.0, the
TryCast()returns theSmtpClientbecause it implements IDisposable.If running in .NET 3.5, the
TryCast()returnsNothingand theUsingis ignored.There does not appear to be any cleanup required for
SmtpClientin .NET 3.5, since they didn’t provide aDispose()or any other cleanup method as far as I can tell.