SendMailErrorEventHandler 委托

定义

表示处理控件(如 ChangePassword 控件、CreateUserWizard 控件以及 PasswordRecovery 控件)的 SendMailError 事件的方法。Represents the method that handles the SendMailError event of controls such as the ChangePassword control, the CreateUserWizard control, and the PasswordRecovery control.

public delegate void SendMailErrorEventHandler(System::Object ^ sender, SendMailErrorEventArgs ^ e);
public delegate void SendMailErrorEventHandler(object sender, SendMailErrorEventArgs e);
type SendMailErrorEventHandler = delegate of obj * SendMailErrorEventArgs -> unit
Public Delegate Sub SendMailErrorEventHandler(sender As Object, e As SendMailErrorEventArgs)

参数

sender
Object

事件源。The source of the event.

e
SendMailErrorEventArgs

包含事件数据的 SendMailErrorEventArgs 对象。A SendMailErrorEventArgs object that contains the event data.

继承
SendMailErrorEventHandler

示例

下面的代码示例演示一个 ASP.NET 页,该页面使用 @no__t 0 Web 控件,并包含名为 SendMailError 的 @no__t 1 事件的事件处理程序。The following code example demonstrates an ASP.NET page that uses a ChangePassword Web control, and includes an event handler for the SendMailError event named SendMailError. 此代码示例假定已将 ASP.NET 网站配置为使用 ASP.NET 成员身份和窗体身份验证,并且已创建一个用户知道其名称和密码。The code example assumes that the ASP.NET Web site has been configured to use ASP.NET membership and Forms authentication, and that a user has been created whose name and password are known to you. 有关详细信息,请参阅[如何:实现简单的 Forms 身份验证 @ no__t。For more information, see How to: Implement Simple Forms Authentication.

如果密码更改成功,则代码会尝试使用 SMTP 向用户发送电子邮件,以确认更改。If the password change succeeds, the code attempts to use SMTP to send an email message to the user to confirm the change. 这是在 SendingMail 事件处理程序中完成的。This is done in the SendingMail event handler. 有关如何配置 SMTP 服务器的信息,请参阅 [How to:在 IIS 6.0 @ no__t 中安装和配置 SMTP 虚拟服务器。For information about how to configure an SMTP server, see How to: Install and Configure SMTP Virtual Servers in IIS 6.0. 出于本示例的目的,不需要配置 SMTP 服务器;该示例构造为测试发送电子邮件失败的情况。For the purposes of this example, it is not necessary to configure an SMTP server; the example is constructed to test for a failure to send an email message.

如果邮件服务器配置不正确或发生其他错误,并且无法发送电子邮件,则会调用 SendMailError 函数。If a mail server is not configured correctly or some other error occurs and the email message cannot be sent, the SendMailError function is called. 向用户显示一条消息。A message is displayed to the user. 此外,将事件记录到 Windows 应用程序事件日志中,假定名为 MySamplesSite 的事件源已存在。In addition, an event is logged to the Windows Application event log with the assumption that an event source named MySamplesSite already exists. 请参阅下面的代码示例,以创建指定的事件源。See the code example below to create the specified event source. 有关创建事件源的详细信息,请参阅ASP.NET Web 窗体页中的服务器事件处理For more information about creating an event source, see Server Event Handling in ASP.NET Web Forms Pages. @No__t 的对象的 @no__t 0 属性设置为 true,指示已处理错误。The Handled property of the SendMailErrorEventArgs object is set to true to indicate that the error has been handled.

<%@ 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>
<%@ Page Language="VB" 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">

  Public Sub MySendingMail(ByVal Sender As Object, ByVal e As MailMessageEventArgs)
    Message1.Text = "Sent mail to you to confirm the password change."
  End Sub

  Public Sub MySendMailError(ByVal Sender As Object, ByVal e As SendMailErrorEventArgs)
    Message1.Text = "Could not send mail to confirm the password change."
    
    ' The MySamplesSite event source has already been created by an administrator.
    Dim myLog As 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
    
  End Sub

</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 的事件源添加到应用程序日志,请使用以下代码示例。Use the following code example if you need to programmatically add the event source named MySamplesSite to your Application log. 要使第一个代码示例正常工作,必须存在此事件源。This event source must exist in order for the first code example to work correctly. 下面的代码示例需要管理员权限。The following code example requires Administrator privileges.

#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());
            }
        }
    }
}
Imports System.Collections.Generic
Imports System.Text
Imports System.Diagnostics


Namespace CreateEventSource
  Class Program
    Sub Main()

        Try
            ' Create the source, if it does not already exist.
            If Not (EventLog.SourceExists("MySamplesSite")) Then
                EventLog.CreateEventSource("MySamplesSite", "Application")
                Console.WriteLine("Creating Event Source")
            End If

            ' Create an EventLog instance and assign its source.
            Dim myLog As 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 e As Exception
            Console.WriteLine("Exception:")
            Console.WriteLine(e.ToString)
        End Try

    End Sub
  End Class
End Namespace

注解

创建 SendMailErrorEventHandler 委托时,需要标识将处理该事件的方法。When you create a SendMailErrorEventHandler delegate, you identify the method that will handle the event. 若要将事件与事件处理程序关联,请将委托的实例添加到事件。To associate the event with the event handler, add an instance of the delegate to the event. 只要事件发生,就会调用事件处理程序,除非从事件中移除委托。The event handler is called whenever the event occurs, unless you remove the delegate from the event. 有关事件处理程序委托的详细信息,请参阅ASP.NET Web 窗体页中的服务器事件处理For more information about event-handler delegates, see Server Event Handling in ASP.NET Web Forms Pages.

如果处理 @no__t 0 事件,则即使在尝试发送电子邮件时发生异常,你的 Web 应用程序仍可以继续运行。Handling the SendMailError event allows your Web application to continue running, even if an exception occurs when trying to send an email message. 例如,如果在用户使用多步骤向导时出现异常,则此方法很有用。For example, this is useful if the exception occurs when a user is working through a multi-step wizard. 更可取的方法是记录错误、向用户显示一条信息性消息,并允许用户完成向导而不是终止应用程序。It is preferable to log the error, display an informative message to the user, and allow the user to complete the wizard rather than terminate the application.

如果没有为 @no__t 0 事件创建事件处理程序,或创建了一个事件处理程序,但将 @no__t 设置为 false,则在发送电子邮件的过程中出现错误时,Web 应用程序将停止运行,并且 ASP.NET 将显示错误消息。If you do not create an event handler for the SendMailError event, or if you create an event handler but leave the Handled property set to false, your Web application will stop running if an error occurs when sending an email message, and ASP.NET will display an error message.

@No__t-0 方法还允许派生类处理事件,而不是 SendMailErrorEventHandlerThe OnSendMailError method also allows derived classes to handle the event instead of the SendMailErrorEventHandler. 这是在派生自 @no__t 0 或 CreateUserWizard 的类中处理事件的首选技术。This is the preferred technique for handling the event in a class that is derived from ChangePassword or CreateUserWizard.

有关处理事件的详细信息,请参阅ASP.NET Web Forms Pages 中的服务器事件处理For more information about handling events, see Server Event Handling in ASP.NET Web Forms Pages.

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于

另请参阅