SendMailErrorEventArgs.Handled 属性

定义

指示包含在 Exception 属性中的 SMTP 异常是否已经得到处理。

C#
public bool Handled { get; set; }

属性值

如果为 true,则此异常已被 SendMailErrorEventHandler 委托使用并得到处理。 如果为 false,则再次引发异常,包含原来的调用堆栈和错误信息。

默认值为 false

示例

下面的代码示例演示了一个使用 ChangePassword Web 控件的 ASP.NET 页,并包含名为 SendMailError的事件的SendMailError事件处理程序。 代码示例假定 ASP.NET 网站已配置为使用成员身份和窗体身份验证 ASP.NET,并且已创建名称和密码已知的用户。 有关详细信息,请参阅 如何:实现简单表单身份验证

如果密码更改成功,事件处理程序中的 SendingMail 代码会尝试向用户发送电子邮件以确认更改。 必须已在服务器上配置 SMTP 才能使此代码示例正常工作。 有关如何配置 SMTP 服务器的信息,请参阅 如何:在 IIS 6.0 中安装和配置 SMTP 虚拟服务器。 就此示例而言,无需配置 SMTP 服务器;构造示例是为了测试发送电子邮件失败。

如果邮件服务器配置不正确或发生其他错误,并且无法发送电子邮件, SendMailError 则调用 函数。 向用户显示一条消息。 此外,将事件记录到 Windows 应用程序事件日志中,假设已存在名为 MySamplesSite 的事件源。 请参阅下面的代码示例来创建指定的事件源。 有关创建事件源的详细信息,请参阅 ASP.NET Web 窗体页中的服务器事件处理。 对象的 Handled 属性 SendMailErrorEventArgs 设置为 以 true 指示已处理错误。

ASP.NET (C#)
<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void MySendingMail(object sender, MailMessageEventArgs e)
  {
    Message1.Text = "Sent mail to you to confirm the password change.";
  }

  void MySendMailError(object sender, SendMailErrorEventArgs e)
  {
    Message1.Text = "Could not send email to confirm password change.";

    // The MySamplesSite event source has already been created by an administrator.
    System.Diagnostics.EventLog myLog = new System.Diagnostics.EventLog();
    myLog.Log = "Application";
    myLog.Source = "MySamplesSite";
    myLog.WriteEntry(
        "Sending mail via SMTP failed with the following error: " + 
        e.Exception.Message.ToString(), 
        System.Diagnostics.EventLogEntryType.Error);

    e.Handled = true;
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>ChangePassword including a SendMailError Event</title>
</head>
<body>
  <form id="form1" runat="server">
  <div style="text-align:center">

    <h1>ChangePassword</h1>
    
    <asp:LoginView ID="LoginView1" Runat="server" 
      Visible="true">
      <LoggedInTemplate>
        <asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." />
        <br />
      </LoggedInTemplate>
      <AnonymousTemplate>
        You are not logged in
      </AnonymousTemplate>
    </asp:LoginView><br />
    
    <asp:ChangePassword ID="ChangePassword1" Runat="server"
      BorderStyle="Solid" 
      BorderWidth="1" 
      CancelDestinationPageUrl="~/Default.aspx" 
      DisplayUserName="true"
      OnSendingMail="MySendingMail" 
      OnSendMailError="MySendMailError" 
      ContinueDestinationPageUrl="~/Default.aspx" >
      <MailDefinition 
        BodyFileName="~\MailFiles\ChangePasswordMail.htm" 
        Subject="Activity information for you">
        <EmbeddedObjects>
          <asp:EmbeddedMailObject Name="LoginGif" Path="~\MailFiles\Login.gif" />
          <asp:EmbeddedMailObject Name="PrivacyNoticeTxt" Path="~\MailFiles\PrivacyNotice.txt" />
        </EmbeddedObjects>
      </MailDefinition>
    </asp:ChangePassword><br />
  
    <asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br />

    <asp:HyperLink ID="HyperLink1" Runat="server" 
      NavigateUrl="~/Default.aspx">
      Home
    </asp:HyperLink>
    
  </div>
  </form>
</body>
</html>

如果需要以编程方式将名为 MySamplesSite 的事件源添加到应用程序日志,请使用以下代码示例。 必须存在此事件源,第一个代码示例才能正常工作。 以下代码示例需要管理员权限。

C#
#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

#endregion

namespace CreateEventSource
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {
                // Create the source, if it does not already exist.
                if (!EventLog.SourceExists("MySamplesSite"))
                {
                    EventLog.CreateEventSource("MySamplesSite", "Application");
                    Console.WriteLine("Creating Event Source");
                }

                // Create an EventLog instance and assign its source.
                EventLog myLog = new EventLog();
                myLog.Source = "MySamplesSite";

                // Write an informational entry to the event log.    
                myLog.WriteEntry("Testing writing to event log.");

                Console.WriteLine("Message written to event log.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception:");
                Console.WriteLine("{0}", e.ToString());
            }
        }
    }
}

注解

属性 Handled 指示 是否已 Exception 处理 。 当控件或 CreateUserWizard无法发送电子邮件ChangePassword时,SMTP 邮件提供程序将引发异常。 出现异常的最常见原因是计算机配置文件的 <smtp> 元素 (网络设置) 中出现配置错误。 尽管在应用程序的开发和调试过程中通常会发现这样的错误,但邮件服务器在生产环境中可能会意外失败,并且必须确定是否希望整个应用程序在这种情况下发生故障。 如果不是,处理事件允许应用程序继续。

如果未为 SendMailError 事件创建事件处理程序,或者创建了事件处理程序,但将 属性设置为 Handledfalse,则如果发送电子邮件时出错,Web 应用程序将停止运行,ASP.NET 将显示错误消息。 处理事件允许 Web 应用程序继续运行,即使发生了异常。 当发送电子邮件并不重要时,这非常有用。 例如,如果用户正在执行多步骤向导时发生异常,则记录错误、向用户显示信息性消息以及允许用户完成向导可能很有利。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅