0

The exact error I am getting in Visual Studio 2012 is:

error BC30456: 'Dispose' is not a member of 'System.Net.Mail.SmtpClient'.

    Dim SmtpServer As New SmtpClient()
    Dim mail As New MailMessage()
    SmtpServer.Port = 25
    SmtpServer.Host = MYHOST.com"
    mail = New MailMessage()
    mail.From = New MailAddress("MYADDRESS@DOMAIN.com")
    mail.To.Add("RECIPIENT@DOMAIN.com")
    mail.Subject = "Test Mail"
    mail.Body = "This is for testing SMTP mail"
    SmtpServer.Send(mail)
    SmtpServer.Dispose()

This should be an obvious error. You would think I was using .net framework 3.5 or lower as Dispose() was only added as a member to SmtpClient in .net 4.0. However, I am using 4.0!

In the website property pages it states 4.0. Is there somewhere else that I need to set as 4.0? Compiler settings somewhere maybe?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
Gravitate
  • 2,885
  • 2
  • 21
  • 37

2 Answers2

0

As you've observed, you are indeed using the 3.5 version of the System.Net.Mail.SmtpClient.

To ensure that you're using version 4:

  • navigate to your project's references director in Visual Studio
  • delete the reference to System.Net
  • right click, Add Reference
  • find the assembly in the list, as below

enter image description here

enter image description here

Also, consider refactoring your code to have a Using block.

Using smtp As New SmtpClient()    
   smtp.Port = 25
   'etc etc
End Using
p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • Thanks for the amazingly quick response. My UI looks very different to yours. I don't have a references folder like you do (because I am working on a website rather than an application?). However, I found my references in the property pages of the site (https://dl.dropbox.com/u/55017434/Capture2.PNG) and you are quite correct, they were all in as 3.5. However, after updating, I am still getting the same error. Is there anywhere else that this can be changed? – Gravitate Jun 21 '12 at 22:22
  • Grav: what happens when you switch to the `Using` block? In that case, you wouldn't have to call `Dispose()` manually. That'd be another clue in whether that compiles correctly in v4. (obv failing if in v3.5). Mind zipping up your project (or stripped down version) for me to download and inspect? – p.campbell Jun 22 '12 at 01:04
  • When using "using", I get basically the same error: error BC36010: 'Using' operand of type 'System.Net.Mail.SmtpClient' must implement 'System.IDisposable'. – Gravitate Jun 22 '12 at 17:52
  • Is there any benefit of using "using"? Or is it personal preference? If you don't mind taking a look, I will zip it up asap. Away this weekend so not sure how long I will have at a computer but will try asap. I will try and cut out as much as possible to narrow it down. I am suspecting it is a bug with VS... If I start a new console application and copy my email code to it, it compiles with no errors. Thanks again for this. – Gravitate Jun 22 '12 at 18:01
  • Grav: no problem. If you post it here, I can take a look. – p.campbell Jun 22 '12 at 18:05
  • p.campbell: I have now solved the problem and posted my fix. However, I shall mark your response as the answer as I believe it would fix the issue for most people, and that my issue was an unlikely bug with VS. Also your response also lead me in the right direction to finding the fix for my issue. Thanks again. :-) – Gravitate Jun 22 '12 at 18:35
  • P.S. for those wondering about "using" and "dispose" see [link](http://stackoverflow.com/questions/10984336/net-using-using-blocks-vs-calling-dispose?rq=1) – Gravitate Jun 22 '12 at 18:46
0

I have now fixed this issue. Not too sure what was actually causing it, but for anyone else experiencing a similar issue, here is how I fixed it.

Go to your property pages. (right click on project --> Property Pages) (or just Shift+F4)

Select "Build" in the left hand box. (Image)

Change the "Target Framework" to ".NET Framework 3.5"

After that finishes, close your project/solution.

Reopen your project and repeat the process, this time selecting ".NET Framework 4.0".

Close and reopen your project once it finishes.

I am guessing this resets all the required references to 4.0. Maybe VS even caches references? And this clears the cache? Not sure, but it worked for me, and it is worth a try if you are having the same issue.

Gravitate
  • 2,885
  • 2
  • 21
  • 37