SendMailErrorEventArgs 類別
定義
提供資料給控制項的 SendMailError
事件,例如 ChangePassword 控制項、CreateUserWizard 控制項 和 PasswordRecovery 控制項。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
- 繼承
範例
下列程式碼範例示範使用 Web 控制項的 ASP.NET 網頁 ChangePassword ,並包含名為 SendMailError 之事件的事件處理常式 SendMailError 。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. 如需詳細資訊,請參閱 如何:執行簡單的表單驗證。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 Forms 頁面中的伺服器事件處理。For more information about creating an event source, see Server Event Handling in ASP.NET Web Forms Pages. Handled物件的屬性(property) 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
備註
SendMailErrorEventArgs物件包含當 ChangePassword 控制項或控制項無法傳送電子郵件訊息時,SMTP 郵件提供者所引發的錯誤訊息 CreateUserWizard 。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. 在這種情況下, SendMailErrorEventArgs 物件會傳送至 SendMailErrorEventHandler 。In such a case, the SendMailErrorEventArgs object is sent to the SendMailErrorEventHandler.
建立 SendMailErrorEventHandler 委派以處理事件。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. 例如,如果例外狀況是在使用者透過多步驟 wizard 進行工作時所發生,則記錄錯誤、向使用者顯示有資訊的訊息,並允許使用者完成嚮導是很有利的。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 > 元素 (網路設定) 中的設定錯誤。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.
如果您未建立事件的事件處理常式 SendMailError ,或如果您建立事件處理常式,但將 Handled 屬性設為,則 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.
OnSendMailError方法也可讓衍生類別處理事件,而不是由完成 SendMailErrorEventHandler 。The OnSendMailError method also allows derived classes to handle the event, instead of this being done by the SendMailErrorEventHandler. 這是在衍生自或的類別中處理事件的慣用技巧 ChangePassword CreateUserWizard 。This is the preferred technique for handling the event in a class that is derived from ChangePassword or CreateUserWizard.
如需處理事件的詳細資訊,請參閱 ASP.NET Web Forms 頁面中的伺服器事件處理。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() |
取得目前執行個體的 Type。Gets the Type of the current instance. (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。Creates a shallow copy of the current Object. (繼承來源 Object) |
ToString() |
傳回代表目前物件的字串。Returns a string that represents the current object. (繼承來源 Object) |
適用於
另請參閱
- SendMailError
- SendMailError
- SendMailError
- MailMessage
- ASP.NET Web Pages 中的伺服器事件處理Server Event Handling in ASP.NET Web Pages
- 處理和引發事件Handling and Raising Events
- 網站管理工具應用程式索引標籤Web Site Administration Tool Application Tab
- 保護登入控制項Securing Login Controls
- Web 應用程式的基本安全性做法Basic Security Practices for Web Applications