SendMailErrorEventArgs 类    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为控件(如ChangePassword 控件、CreateUserWizard 控件和 PasswordRecovery 控件)的 SendMailError 事件提供数据。
public ref class SendMailErrorEventArgs : EventArgspublic class SendMailErrorEventArgs : EventArgstype SendMailErrorEventArgs = class
    inherit EventArgsPublic Class SendMailErrorEventArgs
Inherits EventArgs- 继承
示例
下面的代码示例演示使用 ChangePassword Web 控件的 ASP.NET 页,并包含名为 SendMailError 的事件的事件处理程序 SendMailError 。 该代码示例假定 ASP.NET 网站已配置为使用 ASP.NET 成员身份和 Forms 身份验证,并且已创建一个名称和密码已知的用户。 有关详细信息,请参阅 如何:实现简单窗体身份验证。
如果密码更改成功,事件处理程序中的 SendingMail 代码会尝试向用户发送电子邮件以确认更改。 必须已在服务器上配置 SMTP,此代码示例才能正常工作。 有关如何配置 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
注解
对象 SendMailErrorEventArgs 包含当 控件或 控件无法发送电子邮件 ChangePassword 时 SMTP 邮件提供程序引发的 CreateUserWizard 错误消息。 在这种情况下,对象 SendMailErrorEventArgs 将发送到 SendMailErrorEventHandler。
创建委托 SendMailErrorEventHandler 来处理事件。 处理事件允许 Web 应用程序继续运行,即使发生了异常。 当发送电子邮件并不重要时,这非常有用。 例如,如果用户正在执行多步骤向导时发生异常,则最好记录错误,向用户显示信息性消息,并允许用户完成向导。
Exception检查 属性以确定异常的实际原因。 出现异常的最常见原因是计算机配置文件的 <smtp> 元素 (网络设置) 出现配置错误。 尽管在应用程序的开发和调试过程中通常会发现这样的错误,但在生产环境中,邮件服务器可能会意外失败,并且必须确定是否希望整个应用程序在这种情况下失败。 如果不是,处理 事件允许应用程序继续。
必须将 属性设置为 Handledtrue 以指示已处理异常;否则,将重新引发异常,并将包含原始调用堆栈和错误消息。
如果不为 SendMailError 事件创建事件处理程序,或者如果创建了事件处理程序,但将 Handled 属性设置为 false,则如果发送电子邮件时发生错误,Web 应用程序将停止运行,并且 ASP.NET 将显示错误消息。
方法 OnSendMailError 还允许派生类处理 事件,而不是由 SendMailErrorEventHandler完成此操作。 这是在派生自 ChangePassword 或 CreateUserWizard的类中处理 事件的首选方法。
有关处理事件的详细信息,请参阅 ASP.NET Web Forms Pages 中的服务器事件处理。
继承者说明
在派生类中重写 OnSendMailError(SendMailErrorEventArgs) 时,请务必调用 OnSendMailError(SendMailErrorEventArgs) 基类的 方法,使已注册的委托能够接收事件。
构造函数
| SendMailErrorEventArgs(Exception) | 初始化 SendMailErrorEventArgs 类的新实例。 | 
属性
| Exception | 当无法发送电子邮件时返回由 SMTP 邮件服务引发的异常。 | 
| Handled | 指示包含在 Exception 属性中的 SMTP 异常是否已经得到处理。 | 
方法
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) |