SendMailErrorEventHandler 代理人

定義

ChangePasswordCreateUserWizardPasswordRecovery などの各コントロールの SendMailError イベントを処理するメソッドを表します。

public delegate void SendMailErrorEventHandler(System::Object ^ sender, SendMailErrorEventArgs ^ e);
public delegate void SendMailErrorEventHandler(object sender, SendMailErrorEventArgs e);
type SendMailErrorEventHandler = delegate of obj * SendMailErrorEventArgs -> unit
Public Delegate Sub SendMailErrorEventHandler(sender As Object, e As SendMailErrorEventArgs)

パラメーター

sender
Object

イベントのソース。

e
SendMailErrorEventArgs

イベント データを含んでいる SendMailErrorEventArgs オブジェクト。

次のコード例は、Web コントロールを使用ChangePasswordし、 という名前SendMailErrorのイベントのイベント ハンドラーを含む ASP.NET ページをSendMailError示しています。 このコード例では、ASP.NET Web サイトがメンバーシップとフォーム認証 ASP.NET 使用するように構成されており、名前とパスワードが認識されているユーザーが作成されていることを前提としています。 詳細については、「 方法: 簡易フォーム認証を実装する」を参照してください。

パスワードの変更が成功すると、コードは SMTP を使用してユーザーに電子メール メッセージを送信し、変更を確認しようとします。 これはイベント ハンドラーで SendingMail 行われます。 SMTP サーバーを構成する方法については、「 方法: IIS 6.0 に SMTP 仮想サーバーをインストールして構成する」を参照してください。 この例では、SMTP サーバーを構成する必要はありません。この例は、電子メール メッセージの送信失敗をテストするために構築されています。

メール サーバーが正しく構成されていない場合、または他のエラーが発生し、電子メール メッセージを送信できない場合は、関数 SendMailError が呼び出されます。 ユーザーにメッセージが表示されます。 さらに、MySamplesSite という名前のイベント ソースが既に存在することを前提として、イベントが Windows アプリケーション イベント ログに記録されます。 指定したイベント ソースを作成するには、次のコード例を参照してください。 イベント ソースの作成の詳細については、「ASP.NET Web Forms ページでのサーバー イベント処理」を参照してください。 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

注釈

SendMailErrorEventHandler デリゲートを作成する場合は、イベントを処理するメソッドを指定します。 イベントをイベント ハンドラーに関連付けるには、デリゲートのインスタンスを イベントに追加します。 イベントからデリゲートを削除しない限り、イベントが発生するたびにイベント ハンドラーが呼び出されます。 イベント ハンドラー デリゲートの詳細については、「ASP.NET Web Forms ページでのサーバー イベント処理」を参照してください。

イベントを SendMailError 処理すると、電子メール メッセージを送信しようとしたときに例外が発生した場合でも、Web アプリケーションの実行を続行できます。 たとえば、これは、ユーザーがマルチステップ ウィザードを使用しているときに例外が発生した場合に便利です。 エラーをログに記録し、ユーザーに情報メッセージを表示し、アプリケーションを終了するのではなく、ユーザーがウィザードを完了できるようにすることをお勧めしています。

イベントの SendMailError イベント ハンドラーを作成しない場合、またはイベント ハンドラーを作成し、 プロパティを に設定したまま Handled にすると、電子メール メッセージの送信時に falseエラーが発生すると Web アプリケーションの実行が停止し、ASP.NET エラー メッセージが表示されます。

メソッドを OnSendMailError 使用すると、派生クラスが の代わりに イベントを SendMailErrorEventHandler処理することもできます。 これは、 または CreateUserWizardからChangePassword派生したクラスでイベントを処理するための推奨される手法です。

イベントの処理の詳細については、「ASP.NET Web Forms ページでのサーバー イベント処理」を参照してください。

拡張メソッド

GetMethodInfo(Delegate)

指定したデリゲートによって表されるメソッドを表すオブジェクトを取得します。

適用対象

こちらもご覧ください