Reputation: 123
I'm currently working on a asp.net website using Visual Studio 2010... I'm trying to send email from my contact us page... I'm getting this error:
Warning 9 CA2000 : Microsoft.Reliability : In method 'Default2.Button1_Click(object, EventArgs)', call System.IDisposable.Dispose on object 'msg' before all references to it are out of scope. c:\Users\toshiba\Documents\Visual Studio 2010\WebSites\Carp-MacDental\ContactUs.aspx.cs 29 C:\...\Carp-MacDental\
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
public partial class Default2 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string url = Request.Url.AbsoluteUri;
string hyperlink = "<a href='" + url + "'>" + url + "</a>";
NetworkCredential loginInfo = new NetworkCredential("[email protected]", "password");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]");
msg.To.Add(new MailAddress(txtEmail.Text));
msg.Bcc.Add(new MailAddress("[email protected]"));
msg.Subject = "Notification from:" + url;
msg.Body = "A message form," + txtName.Text + ", <br/>" + txtMessage.Text;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com");
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";
}
}
Its my first time working with this so I'm pretty confused.. Also my mail message that was supposed to be sent through the website wasn't sent...
Upvotes: 0
Views: 453
Reputation: 9166
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.
The problem is that you are creating a couple of instances of classes that implement the IDisposable
interface. Classes that implement this interface have a Dispose
method that should be called when you are finished with the instance. The Dispose
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.
Both MailMessage
and SmtpClient
implement this interface. So intances of these classes should have their respective Dispose
method called before they go out of scope. The first thought might be to simply add a couple of lines to you existing code:
...
client.Send(msg);
msg.Dispose();
client.Dispose();
...
But a better soultion would probably be to wrap these in using() { }
blocks. Doing this will make the compiler insert code that automatically calls the Dispose
method, and it will even be called even if there is an exception thrown from inside the using() { }
block. Try this:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string url = Request.Url.AbsoluteUri;
string hyperlink = "<a href='" + url + "'>" + url + "</a>";
NetworkCredential loginInfo = new NetworkCredential("[email protected]", "password");
using(MailMessage msg = new MailMessage())
using(SmtpClient client = new SmtpClient("smtp.gmail.com"))
{
msg.From = new MailAddress("[email protected]");
msg.To.Add(new MailAddress(txtEmail.Text));
msg.Bcc.Add(new MailAddress("[email protected]"));
msg.Subject = "Notification from:" + url;
msg.Body = "A message form," + txtName.Text + ", <br/>" + 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";
}
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.
Upvotes: 2