SendMailErrorEventHandler 委托

定义

表示处理控件(如 ChangePassword 控件、CreateUserWizard 控件以及 PasswordRecovery 控件)的 SendMailError 事件的方法。

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

事件源。

e
SendMailErrorEventArgs

包含事件数据的 SendMailErrorEventArgs 对象。

示例

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

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

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

<%@ 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 的事件源添加到应用程序日志,请使用以下代码示例。 必须存在此事件源,第一个代码示例才能正常工作。 下面的代码示例需要管理员权限。

#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 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序相关联,请将 委托的实例添加到 事件。 每当事件发生时,将调用 事件处理程序,除非从事件中删除委托。 有关事件处理程序委托的详细信息,请参阅 ASP.NET Web Forms Pages 中的服务器事件处理

SendMailError处理 事件可让 Web 应用程序继续运行,即使尝试发送电子邮件时出现异常也是如此。 例如,如果用户正在执行多步骤向导时发生异常,则这非常有用。 最好记录错误,向用户显示信息性消息,并允许用户完成向导,而不是终止应用程序。

如果不为 SendMailError 事件创建事件处理程序,或者如果创建了事件处理程序,但将 Handled 属性设置为 false,则如果发送电子邮件时发生错误,Web 应用程序将停止运行,并且 ASP.NET 将显示错误消息。

方法 OnSendMailError 还允许派生类处理 事件而不是 SendMailErrorEventHandler。 这是在派生自 ChangePasswordCreateUserWizard的类中处理 事件的首选方法。

有关处理事件的详细信息,请参阅 ASP.NET Web Forms Pages 中的服务器事件处理

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

另请参阅