Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Global.asax文件发送电子邮件_C#_Asp.net_Unhandled Exception - Fatal编程技术网

C# 使用Global.asax文件发送电子邮件

C# 使用Global.asax文件发送电子邮件,c#,asp.net,unhandled-exception,C#,Asp.net,Unhandled Exception,如果发生未经处理的错误,我想向管理员发送一封电子邮件,其中包含所发生错误的信息。下面是我的web.config和Global.asax.cs文件中的内容,重定向有效,但电子邮件无效: <system.web> <customErrors mode="On" defaultRedirect="error.aspx" /> </system.web> void Application_Error(object sender, EventArgs e)

如果发生未经处理的错误,我想向管理员发送一封电子邮件,其中包含所发生错误的信息。下面是我的web.config和Global.asax.cs文件中的内容,重定向有效,但电子邮件无效:

<system.web>
    <customErrors mode="On" defaultRedirect="error.aspx" />
</system.web>

void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs
            // Get the exception object.
            Exception error = Server.GetLastError();

            MailMessage mail = new MailMessage();
            mail.To.Add("admin@mysite.com");
            mail.Subject = "Error";
            mail.Body = "Somebody has experienced an error." + "<br><br>";
            mail.Body += error.ToString();

            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("username", "password");
            smtp.Port = 587;
            smtp.Send(mail);

            Server.ClearError();
        }

无效应用程序错误(对象发送方,事件参数e)
{
//发生未处理错误时运行的代码
//获取异常对象。
异常错误=Server.GetLastError();
MailMessage mail=新的MailMessage();
mail.To.Add(“admin@mysite.com");
mail.Subject=“Error”;
mail.Body=“有人遇到错误。”+”

”; mail.Body+=error.ToString(); mail.IsBodyHtml=true; SmtpClient smtp=新SmtpClient(); smtp.Host=“smtp.gmail.com”; smtp.EnableSsl=true; smtp.Credentials=新系统.Net.NetworkCredential(“用户名”、“密码”); smtp.Port=587; smtp.发送(邮件); ClearError(); }
默认情况下,所有未捕获的异常都将记录到Windows事件日志中。您可以在
控制面板
->
管理工具
->
事件查看器
->
Windows日志
->
应用程序

中访问它,您应该安装(您可以通过它来完成)并让它为您完成工作。最好的代码是你不写的代码

Elmah是一个HttpModule,位于ASP.Net堆栈的顶部,用于捕获/处理未捕获的异常:

ELMAH(错误记录模块和处理程序)是一种应用程序范围的错误记录工具 这是完全可插拔的。它可以动态添加到正在运行的ASP.NET web 应用程序,甚至是机器上的所有ASP.NET web应用程序,而无需 重新编译或重新部署

一旦ELMAH被放入正在运行的web应用程序并进行了适当的配置, 您可以在不更改一行代码的情况下获得以下功能:

  • 记录几乎所有未处理的异常
  • 远程查看重新编码异常的整个日志的网页
  • 远程查看任何一个已记录异常的完整详细信息的网页,包括 彩色堆叠痕迹
  • 在许多情况下,您可以查看ASP.NET提供的原始死亡黄屏 为给定异常生成,即使customErrors模式已关闭
  • 每个错误发生时的电子邮件通知
  • 日志中最近15个错误的RSS提要
将Elmah配置为发送电子邮件(在记录电子邮件的基础上)非常简单


如果您要编写自己的日志,请安装并配置它,使其除了使用您想要/需要/想要的任何其他日志附加程序外,还可以使用1个或多个日志附加程序。

以何种方式“电子邮件不起作用”?请更具体一点。你没有在
smtp.Send(mail)上收到异常吗?电子邮件未发送,除error.aspx页面上的默认消息外,未显示任何错误消息。似乎应用程序重定向到错误页面,而对Global.asax不执行任何操作。您是否可以在Server.GetLastError()之后立即检查var错误中的null,这将避免error.ToString()出现null异常。同时尝试删除defaultRedirect。我发现了这个,它解决了我的问题,至少现在是这样:@Kristopher and prey tell,你建议如何调试不发送的电子邮件?我擅长调试,但我不能通灵调试这个问题…现在,在2016年,使用ELMAH是好的模式和实践吗?
protected void Application_Error(Object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    EmailTheException( ex );
}

private void EmailTheException( Exception ex )
{
    MailMessage mail = new MailMessage();
    mail.To = "GroupEmail@SomeCompany.com";
    mail.From = "SomeApplication@SomeCompany.com";
    mail.Subject = "An Application Exception has Occurred.";
    mail.Body = ex.ToString();

    var smtpHost = ConfigurationManager.AppSettings["smtphost"];
    var port = Convert.ToUInt32(ConfigurationManager.AppSettings["port"]);
    using (SmtpClient client = new SmtpClient(smtpHost))
    {
        if (string.IsNullOrEmpty(smtpHost)) return;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        try
        {
            client.Send(mail);
        }
        catch (SmtpException smtpEx)
        {
           //write logging code here and capture smtpEx.Message
        }
    }
}