如何通过电子邮件发送ASP.NET错误

如何通过电子邮件发送ASP.NET错误,asp.net,error-handling,Asp.net,Error Handling,我在123 Reg上托管了一个网站,目前向用户显示我的错误消息——我过去曾要求他们向我发送屏幕截图,以便我可以调试正在发生的事情 但是,我想设置一个带有一些文本的自定义错误页面,然后将错误通过电子邮件发送到我的收件箱。然而,我在网上发现的任何东西似乎都建议对IIS进行更改 由于使用123 Reg托管,我无法访问IIS。这可以在应用程序级别实现吗?您可以轻松添加Elmah: 右键单击项目,选择“管理Nuget软件包” 点击“在线”->“NuGet官方软件包源” 在搜索框中键入Elmah,然后选择第

我在123 Reg上托管了一个网站,目前向用户显示我的错误消息——我过去曾要求他们向我发送屏幕截图,以便我可以调试正在发生的事情

但是,我想设置一个带有一些文本的自定义错误页面,然后将错误通过电子邮件发送到我的收件箱。然而,我在网上发现的任何东西似乎都建议对IIS进行更改


由于使用123 Reg托管,我无法访问IIS。这可以在应用程序级别实现吗?

您可以轻松添加Elmah:

  • 右键单击项目,选择“管理Nuget软件包”
  • 点击“在线”->“NuGet官方软件包源”
  • 在搜索框中键入Elmah,然后选择第一次点击。点击安装
  • 通过定位以下行并将其更改为“远程访问”,在web.config中启用“远程访问”:

    <elmah>
    <!--
        See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
        more information on remote access and securing ELMAH.
     -->
    <security allowRemoteAccess="true" />
    </elmah>
    
    
    
  • 您几乎完成了:请确保按照上面URL中的说明保护错误页面,并防止未经授权访问您的日志

    更新我应该说,上面只会在Elmah仪表板上显示错误。您可以将其配置为也发送电子邮件错误。您将在此处找到一个如何执行此操作的示例:


    在Global.asax页面上,只需添加以下代码即可

     void Application_Error(object sender, EventArgs e) 
       { 
            // Code that runs when an unhandled error occurs
            // Get the error details 
            HttpException lastErrorWrapper = Server.GetLastError() as HttpException;
    
            Exception lastError = lastErrorWrapper;
            if (lastErrorWrapper.InnerException != null)
                lastError = lastErrorWrapper.InnerException;
    
            string lastErrorTypeName = lastError.GetType().ToString();
            string lastErrorMessage = lastError.Message;
            string lastErrorStackTrace = lastError.StackTrace;
    
            const string ToAddress = "youremail@yourdomain.com";
            const string FromAddress = "noreply@yourdomain.com";
            const string Subject = "An Error Has Occurred on Application Name!";
    
            // Create the MailMessage object 
            MailMessage msg = new MailMessage();
            string[] eAddresses = ToAddress.Split(';');
    
            for (int i = 0; i < eAddresses.Length; i++)
            {
                if (eAddresses[i].Trim() != "")
                {
                    msg.To.Add(new MailAddress(eAddresses[i]));
    
                }
            }
            msg.From = (new MailAddress(FromAddress));
    
            msg.Subject = Subject;
            msg.IsBodyHtml = true;
            msg.Priority = MailPriority.High;
            msg.Body = string.Format(@" 
    <html> 
    <body> 
      <h1>An Error Has Occurred!</h1> 
      <table cellpadding=""5"" cellspacing=""0"" border=""1""> 
      <tr> 
      <td text-align: right;font-weight: bold"">URL:</td> 
      <td>{0}</td> 
      </tr> 
      <tr> 
      <td text-align: right;font-weight: bold"">User:</td> 
      <td>{1}</td> 
      </tr> 
      <tr> 
      <td text-align: right;font-weight: bold"">Exception Type:</td> 
      <td>{2}</td> 
      </tr> 
      <tr> 
      <td text-align: right;font-weight: bold"">Message:</td> 
      <td>{3}</td> 
      </tr> 
      <tr> 
      <td text-align: right;font-weight: bold"">Stack Trace:</td> 
      <td>{4}</td> 
      </tr>  
      </table> 
    </body> 
    </html>",
                Request.Url,
                Request.ServerVariables["LOGON_USER"],
                lastErrorTypeName,
                lastErrorMessage,
                lastErrorStackTrace.Replace(Environment.NewLine, "<br />"));
    
    
            // Attach the Yellow Screen of Death for this error    
            string YSODmarkup = lastErrorWrapper.GetHtmlErrorMessage();
            if (!string.IsNullOrEmpty(YSODmarkup))
            {
                Attachment YSOD = Attachment.CreateAttachmentFromString(YSODmarkup, "YSOD.htm");
                msg.Attachments.Add(YSOD);
            }
    
    
            // Send the email 
            SmtpClient smtp = new SmtpClient();
            smtp.Send(msg);
    
            Server.Transfer("/Error.aspx");
    
        }
    
    void应用程序\u错误(对象发送方,事件参数e)
    { 
    //发生未处理错误时运行的代码
    //获取错误详细信息
    HttpException lastErrorWrapper=Server.GetLastError()作为HttpException;
    异常lastError=lastErrorWrapper;
    if(lastErrorWrapper.InnerException!=null)
    lastError=lastErrorWrapper.InnerException;
    字符串lastErrorTypeName=lastError.GetType().ToString();
    字符串lastErrorMessage=lastError.Message;
    字符串lastErrorStackTrace=lastError.StackTrace;
    常量字符串到地址=”youremail@yourdomain.com";
    常量字符串FromAddress=”noreply@yourdomain.com";
    const string Subject=“应用程序名称出现错误!”;
    //创建MailMessage对象
    MailMessage msg=新的MailMessage();
    字符串[]eadresses=ToAddress.Split(“;”);
    for(int i=0;i”);
    //为此错误附加黄色死亡屏幕
    字符串YSODmarkup=lastErrorWrapper.GetHtmlErrorMessage();
    如果(!string.IsNullOrEmpty(YSODmarkup))
    {
    附件YSOD=Attachment.CreateAttachmentFromString(YSODmarkup,“YSOD.htm”);
    msg.Attachments.Add(YSOD);
    }
    //发送电子邮件
    SmtpClient smtp=新SmtpClient();
    smtp.Send(msg);
    Server.Transfer(“/Error.aspx”);
    }
    
    如果您可以在smtp服务器上打开tcp套接字到端口25,您可以发送电子邮件。我让我的应用程序发送电子邮件。我只是不知道如何从应用程序中定义自定义错误页。。。