我目前正在使用 Visual Studio 2010 在 asp.net 网站上工作...我正在尝试从我的联系我们页面发送电子邮件...我收到此错误:
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\
这是我的代码:
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("Site@gmail.com", "password");
MailMessage msg = new MailMessage();
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 + ", <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";
}
}
这是我第一次使用这个,所以我很困惑..我应该通过网站发送的邮件也没有发送......