简体   繁体   English

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

[英]Error in sending email through asp.net

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: 我目前正在使用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\\ 

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("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";
    }
}

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... 这是我第一次使用这个,所以我很困惑..我的邮件本应该通过网站发送的邮件没有发送...

As Aristos indicated with his link to Msdn, the warning you see is a code analysis warning. 正如Aristos指出他与Msdn的链接一样,你看到的警告是代码分析警告。 Visual Studio has identified that there is a small problem with your code and shows the warning to alert you about the problem. Visual Studio已确定您的代码存在一个小问题,并显示警告以提醒您该问题。

The problem is that you are creating a couple of instances of classes that implement the IDisposable interface. 问题是您正在创建几个实现IDisposable接口的类实例。 Classes that implement this interface have a Dispose method that should be called when you are finished with the instance. 实现此接口的类具有Dispose方法,该方法应在完成实例时调用。 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. Dispose方法用于清除实例在场景后面使用的非托管资源,从而确保释放内存和其他非托管资源以供其他进程使用。

Both MailMessage and SmtpClient implement this interface. MailMessageSmtpClient实现了此接口。 So intances of these classes should have their respective Dispose method called before they go out of scope. 因此,这些类的内容应该在它们超出范围之前调用它们各自的Dispose方法。 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. 但更好的灵魂可能是将这些包装在using() { }块中。 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. 这样做会使编译器插入自动调用Dispose方法的代码,即使从using() { }块内部抛出异常,它也会被调用。 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("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";
}

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. 如果您需要帮助,我建议您发布一个新问题,并在该问题中尽可能多地添加有关问题的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM