SendMailErrorEventArgs 类

定义

为控件(如ChangePassword 控件、CreateUserWizard 控件和 PasswordRecovery 控件)的 SendMailError 事件提供数据。Provides data for the SendMailError event of controls such as the ChangePassword control, the CreateUserWizard control, and the PasswordRecovery control.

public ref class SendMailErrorEventArgs : EventArgs
public class SendMailErrorEventArgs : EventArgs
type SendMailErrorEventArgs = class
    inherit EventArgs
Public Class SendMailErrorEventArgs
Inherits EventArgs
继承
SendMailErrorEventArgs

示例

下面的代码示例演示一个 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.

如果密码更改成功,则 @no__t 0 事件处理程序中的代码会尝试向用户发送电子邮件以确认更改。If the password change succeeds, the code in the SendingMail event handler attempts to send an email message to the user to confirm the change. 为了使此代码示例正常运行,必须已在服务器上配置 SMTP。SMTP must already be configured on the server in order for this code example to work. 有关如何配置 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

注解

当 @no__t 1 控件或 @no__t 2 控件无法发送电子邮件时,@no__t 0 对象包含由 SMTP 邮件提供程序引发的错误消息。The SendMailErrorEventArgs object contains an error message that is raised by the SMTP mail provider when an email message cannot be sent by the ChangePassword control, or the CreateUserWizard control. 在这种情况下,会将 @no__t 0 对象发送到 @no__t。In such a case, the SendMailErrorEventArgs object is sent to the SendMailErrorEventHandler.

创建一个 @no__t 0 的委托来处理事件。Create a SendMailErrorEventHandler delegate to handle the event. 即使发生异常,处理事件也能使您的 Web 应用程序继续运行。Handling the event allows your Web application to continue to run even though an exception has occurred. 如果发送电子邮件不重要,这会很有用。This is useful when it is not critical to send an email message. 例如,如果在用户使用多步骤向导时出现异常,则记录该错误、向用户显示一条信息性消息,并允许用户完成向导会有好处。For example, if the exception occurs when a user is working through a multi-step wizard, it can be advantageous to log the error, display an informative message to the user, and allow the user to complete the wizard.

检查 Exception 属性以确定异常的实际原因。Examine the Exception property to determine the actual cause of the exception. 异常的最常见原因是计算机配置文件的<smtp @ no__t-2 元素(网络设置)中的配置错误。The most common reason for the exception is a configuration error in the <smtp> Element (Network Settings) of the machine configuration file. 尽管在应用程序的开发和调试过程中通常会发现类似于这样的错误,但在生产环境中,邮件服务器可能会意外失败,您必须确定在这种情况下是否希望整个应用程序失败。Although an error like this is typically discovered during the development and debugging of an application, mail servers can fail unexpectedly in a production environment, and you must determine whether you want the entire application to fail in that situation. 如果不是,则处理事件会使应用程序继续运行。If not, handling the event allows your application to proceed.

必须将 Handled 属性设置为 true,以指示已处理异常;否则,将重新引发异常,并将包含原始调用堆栈和错误消息。You must set the Handled property to true to signal that the exception has been handled; otherwise, the exception is rethrown, and will include the original call stack and error message.

如果没有为 @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 方法还允许派生类处理事件,而不是由 SendMailErrorEventHandler 实现。The OnSendMailError method also allows derived classes to handle the event, instead of this being done by 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.

继承者说明

在派生类中重写 OnSendMailError(SendMailErrorEventArgs) 时,请确保调用基类的 OnSendMailError(SendMailErrorEventArgs) 方法,以允许注册的委托接收事件。When overriding OnSendMailError(SendMailErrorEventArgs) in a derived class, be sure to call the OnSendMailError(SendMailErrorEventArgs) method of the base class to enable registered delegates to receive the event.

构造函数

SendMailErrorEventArgs(Exception)

初始化 SendMailErrorEventArgs 类的新实例。Initializes a new instance of the SendMailErrorEventArgs class.

属性

Exception

当无法发送电子邮件时返回由 SMTP 邮件服务引发的异常。Returns the exception thrown by an SMTP mail service when an email message cannot be sent.

Handled

指示包含在 Exception 属性中的 SMTP 异常是否已经得到处理。Indicates if the SMTP exception that is contained in the Exception property has been handled.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅