0

我目前正在使用 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";
    }
}

这是我第一次使用这个,所以我很困惑..我应该通过网站发送的邮件也没有发送......

4

1 回答 1

2

正如 Aristos 在他的 Msdn 链接中指出的那样,您看到的警告是代码分析警告。Visual Studio 已确定您的代码存在小问题,并显示警告以提醒您该问题。

问题是您正在创建几个实现IDisposable接口的类实例。实现此接口的类有一个Dispose方法,当您完成实例时应该调用该方法。该Dispose方法用于清理实例在后台使用的非托管资源,从而确保释放内存和其他非托管资源以供其他进程使用。

两者都MailMessage实现SmtpClient了这个接口。所以这些类的实例应该Dispose在它们超出范围之前调用它们各自的方法。第一个想法可能是简单地在现有代码中添加几行:

        ...
        client.Send(msg);
        msg.Dispose();
        client.Dispose();
        ...

但更好的方法可能是将这些包装成using() { }块。这样做会使编译器插入自动调用该Dispose方法的代码,即使using() { }块内部抛出异常,它也会被调用。尝试这个:

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");

        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 + ", <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";
}

这应该有望为您摆脱编译器警告。电子邮件没有真正发送的事实是另一回事。如果没有更多细节,我无能为力。如果您需要帮助,我建议您发布一个新问题并在该问题中添加尽可能多的有关该问题的信息。

于 2013-06-28T09:39:59.530 回答