通过asp.net发送电子邮件时出错

时间:2013-06-28 07:50:22

标签: asp.net visual-studio-2010 web

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

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

1 个答案:

答案 0 :(得分:2)

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

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

MailMessageSmtpClient都实现此接口。因此,这些类的内容应该在它们超出范围之前调用它们各自的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";
}

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