SendMailErrorEventArgs.Exception 属性

定义

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

public:
 property Exception ^ Exception { Exception ^ get(); void set(Exception ^ value); };
public Exception Exception { get; set; }
member this.Exception : Exception with get, set
Public Property Exception As Exception

属性值

Exception

包含此异常的 Exception 对象。An Exception object that contains the exception.

示例

下面的代码示例演示了一个使用 Web 控件的 ASP.NET 页面 ChangePassword ,并包含名为的事件的事件处理程序 SendMailError SendMailErrorThe 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. 有关详细信息,请参阅 如何:实现简单的窗体身份验证For more information, see How to: Implement Simple Forms Authentication.

如果密码更改成功, SendingMail 事件处理程序中的代码会尝试向用户发送一封电子邮件,以确认所做的更改。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 服务器的信息,请参阅 如何:在 IIS 6.0 中安装和配置 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. Handled对象的属性 SendMailErrorEventArgs 设置为 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

注解

Exception属性包含当控件或无法发送电子邮件时,SMTP 邮件提供程序引发的异常 ChangePassword CreateUserWizardThe Exception property contains the exception that is thrown by the SMTP mail provider when an email message cannot be sent by the ChangePassword control or the CreateUserWizard. 此异常的最常见原因是 (计算机配置文件的 " < > 网络设置") 的 smtp 元素中出现配置错误,这会生成以下异常消息:The transport failed to connect to the server.The most common reason for this exception is a configuration error in the <smtp> Element (Network Settings) of the machine configuration file, which produces the following exception message: The transport failed to connect to the server.

如果使用嵌入文件时电子邮件中存在错误,则不会引发异常 EmbeddedObjectsExceptions are not thrown if there is an error in the email message when embedding a file using EmbeddedObjects. 而是在查看邮件时嵌入的文件显示为 "已损坏"。Instead, the embedded file appears broken when the mail message is viewed.

您必须将 Handled 作为的参数传递的对象设置 e SendMailErrorEventArgs 为,以 true 指示已处理异常; 否则,将重新引发异常,并包含原始调用堆栈和错误消息。You must set the Handled object, passed as the e parameter of SendMailErrorEventArgs, to true to signal that the exception has been handled; otherwise, the exception is rethrown, and includes the original call stack and error message.

适用于

另请参阅