SendMailErrorEventArgs 類別

定義

提供資料給控制項的 SendMailError 事件,例如 ChangePassword 控制項、CreateUserWizard 控制項 和 PasswordRecovery 控制項。

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

範例

下列程式碼範例示範使用 ChangePassword Web 控制項的 ASP.NET 網頁,並包含名為 SendMailError 之事件的事件處理常式 SendMailError 。 程式碼範例假設 ASP.NET 網站已設定為使用 ASP.NET 成員資格和表單驗證,而且已建立使用者的名稱和密碼已知。 如需詳細資訊,請參閱 如何:實作簡單表單驗證

如果密碼變更成功,事件處理常式中的 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 電子郵件訊息或 CreateUserWizard 控制項時,SMTP 郵件提供者所引發的錯誤訊息。 在這種情況下,物件 SendMailErrorEventArgs 會傳送至 SendMailErrorEventHandler

建立 SendMailErrorEventHandler 委派以處理事件。 處理事件可讓您的 Web 應用程式繼續執行,即使發生例外狀況也一樣。 這在傳送電子郵件訊息並不重要時很有用。 例如,如果使用者透過多步驟精靈工作時發生例外狀況,則記錄錯誤、向使用者顯示資訊訊息,以及允許使用者完成精靈會很有説明。

Exception檢查 屬性以判斷例外狀況的實際原因。 例外狀況最常見的原因是在電腦群組態檔的 smtp > 元素 (網路設定) 發生 <設定錯誤。 雖然在開發及偵錯應用程式期間通常會發現類似這樣的錯誤,但郵件伺服器在生產環境中可能會意外失敗,而且您必須判斷是否要在這種情況中整個應用程式失敗。 如果沒有,處理事件可讓應用程式繼續進行。

您必須將 Handled 屬性設定為 true ,以發出已處理例外狀況的訊號,否則會重新擲回例外狀況,並包含原始呼叫堆疊和錯誤訊息。

如果您未建立 SendMailError 事件的事件處理常式,或如果您建立事件處理常式,但將 屬性設定 falseHandled ,則 Web 應用程式會在傳送電子郵件訊息時發生錯誤時停止執行,而 ASP.NET 會顯示錯誤訊息。

方法 OnSendMailError 也允許衍生類別處理事件,而不是由 SendMailErrorEventHandler 完成此動作。 這是在衍生自 ChangePasswordCreateUserWizard 的類別中處理事件的慣用技巧。

如需處理事件的詳細資訊,請參閱ASP.NET Web Forms 頁面中的伺服器事件處理

給繼承者的注意事項

在衍生類別中覆 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)

適用於

另請參閱