ASP.NET页面错误处理及邮件发送简易方案

FROM :http://space.itpub.net/12639172/viewspace-526837

1包含页面:Default.aspx,Error.aspx

2.思路:Global.asax页面负责捕捉系统中除去try以外发 生的页面错误。并将错信息发送给Error.aspx页面。Error.aspx页面负责显示错误信息,并将错误信息发送到指定邮箱。

3. 具体代码

Default.aspx 页面

 

Code
html部分:
<body> ITPUB个人空间O6d!a5T8F�QS W%^
    <form. id="form1" runat=" server">
    <div>
   
axl8C'o B9Ex \;t0    </div>
"GhV Q K%BB"p0    <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Name"
        DataValueField="id">

    </asp:DropDownList>

<asp:Button ID="Button1" runat="server" Text="Button" nClick="Button1_Click" />
    </form>
</body>
cs部分:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("id",typeof(string)));ITPUB个人空间 p3mfkp
            dt.Columns.Add(new DataColumn("name", typeof(string)));

            dt.Rows.Add(dt.NewRow());
            dt.Rows[0][0] = "1";
            dt.Rows[0][1] = "1";ITPUB个人空间J8H"j(gI;D F*by
            this.DropDownList1.DataSource = dt;
            this.DropDownList1.DataBind();

        }
    } ITPUB 个人空间_S u�^s+U
 protected void Button1_Click(object sender, EventArgs e)
    {
        this.DropDownList1.SelectedValue = "fff";
z@ |h._ V8J6H0

    }ITPUB个人空间(U+{/r4| t8v$?8}7C

 

Global.asax代码:

Code
+X%AnN C9o4N6},l:{0<%@ Import Namespace ="System.Web" %>
 void Application_Error(object sender, EventArgs e) ITPUB个人空间m xjkrp%nza
    {
6Zkk;_d3V H)w1o0        Exception  LastError = Server.GetLastError();
        if (LastError != null)
            Response.Redirect("error.aspx?error="+LastError.InnerException.ToString().Replace("\r\n",""));
    } 

 

 

Error.aspx代码:

 

CodeITPUB个人空 间e#TO-NagA
html部分:ITPUB个人空间 T$Y"D[`MEf VW
<body>ITPUB 个人空间*j |\R#f
    <form. id="form1" runat="server">ITPUB个人空间]4dAc z$EKg Z*M
    <div style="background-color: #99CCFF; height: 252px;">ITPUB个人空间)[6R$D'B O(s/Cc
        抱歉:发生了错误。
VB4@]7QP,c0     <div style="background-color:Silver"><asp:Label ID="Label1" runat="server"ITPUB个人空间n(_@ t*W?�E/C5M7g
            Text="Label"></asp:Label></div>
   
[&e z#f"i4ey@ g U6oD0    </div>
iZ~@4YKQP)I*[0    </form>ITPUB 个人空间 cP(a&FV4W
</body>
cs 部分:

添加命名空间:

using System.Net.Mail;ITPUB 个人空间!K9W:R'K.r~ Ne
protected void Page_Load(object sender, EventArgs e)ITPUB个人空间N w0|t;w V
    {
        if (!IsPostBack)
F4gQ#_ rq0        {
            if (Request["error"] != null && Request["error"].Length > 0)
            {
                this.Label1.Text = Request["error"];ITPUB个 人空间4ob;M`.k�fvW*x
                SendMail(Request["error"]);
&MP5dt"{N }-TYh0            }
        }
    }
 public void SendMail(string body)ITPUB个人空间~ {$Z~/nC m;m
    {
        MailMessage myMail = new MailMessage();
       
        myMail.From = new MailAddress("[email protected]");
        myMail.To.Add("[email protected]");
        myMail.Subject = "Error";
        myMail.Priority = MailPriority.Normal;ITPUB 个人空间3r7E,?0Tc-\nz8t
        myMail.BodyEncoding = System.Text.Encoding.UTF8;
        myMail.Body = body;
TtZ#Q moq0X"Skg0        SmtpClient smtp = new SmtpClient();
        smtp.Host = "mail";ITPUB个人空间 W�rfB,GD1W
        try
_t G$Z n�C0        {ITPUB个人空间}W+n l+H R F9Vc+H8R0B
            smtp.Send(myMail);
        }
q ib _3qM|w0        catch (SmtpException ex)
        {
            this.Label1.Text = "邮件发送失败。\r\n"+ex.Message;ITPUB个人空间2] g8Y&l:G_(]9U,L k
        }

    }

 

 至此,系统即可实现错误捕捉显示,及邮件发生功能。

 

ITPUB个人空间\&q ITPUB个人空间\&q

你可能感兴趣的:(asp.net)