Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As Aristos indicated with his link to Msdn, the warning you see is a code analysis warning. Visual Studio has identified that there is a small problem with your code and shows the warning to alert you about the problem.</p> <p>The problem is that you are creating a couple of instances of classes that implement the <code>IDisposable</code> interface. Classes that implement this interface have a <code>Dispose</code> method that should be called when you are finished with the instance. The <code>Dispose</code> method is used to clean up unmanaged resources that the instance is using behind the scene, thus making sure that memory and other unmanaged resources are freed up for use by other processes.</p> <p>Both <code>MailMessage</code> and <code>SmtpClient</code> implement this interface. So intances of these classes should have their respective <code>Dispose</code> method called before they go out of scope. The first thought might be to simply add a couple of lines to you existing code:</p> <pre><code> ... client.Send(msg); msg.Dispose(); client.Dispose(); ... </code></pre> <p>But a better soultion would probably be to wrap these in <code>using() { }</code> blocks. Doing this will make the compiler insert code that automatically calls the <code>Dispose</code> method, and it will even be called even if there is an exception thrown from inside the <code>using() { }</code> block. Try this:</p> <pre><code>protected void Button1_Click(object sender, EventArgs e) { try { string url = Request.Url.AbsoluteUri; string hyperlink = "&lt;a href='" + url + "'&gt;" + url + "&lt;/a&gt;"; NetworkCredential loginInfo = new NetworkCredential("Site@gmail.com", "password"); using(MailMessage msg = new MailMessage()) using(SmtpClient client = new SmtpClient("smtp.gmail.com")) { msg.From = new MailAddress("Site@gmail.com"); msg.To.Add(new MailAddress(txtEmail.Text)); msg.Bcc.Add(new MailAddress("Site@gmail.com")); msg.Subject = "Notification from:" + url; msg.Body = "A message form," + txtName.Text + ", &lt;br/&gt;" + txtMessage.Text; msg.IsBodyHtml = true; client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = loginInfo; client.Send(msg); } } catch (Exception ex) { lblMessage.Text = ex.Message; } lblMessage.Text = "Thank you for contacting us.. we will get back to you as soon as we read your message"; } </code></pre> <p>That should hopefully get rid of the compiler warning for you. The fact that the email is not really sent is another matter. I cannot help with that without a lot more details. If you need help with that, I suggest you post a new question and add as much info about the problem as possible in that question.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload