ChangePassword.OnSendMailError(SendMailErrorEventArgs) 메서드

정의

이메일 메시지를 사용자에게 보낼 수 없는 경우에 SendMailError 이벤트를 발생시킵니다.

protected:
 virtual void OnSendMailError(System::Web::UI::WebControls::SendMailErrorEventArgs ^ e);
protected virtual void OnSendMailError (System.Web.UI.WebControls.SendMailErrorEventArgs e);
abstract member OnSendMailError : System.Web.UI.WebControls.SendMailErrorEventArgs -> unit
override this.OnSendMailError : System.Web.UI.WebControls.SendMailErrorEventArgs -> unit
Protected Overridable Sub OnSendMailError (e As SendMailErrorEventArgs)

매개 변수

e
SendMailErrorEventArgs

이벤트 데이터가 들어 있는 SendMailErrorEventArgs 개체입니다.

예제

다음 코드 예제를 사용 하는 ASP.NET 페이지를 보여 줍니다.는 ChangePassword 웹 컨트롤 및 이벤트 처리기를 포함 합니다 SendingMail 라는 이벤트 SendingMail합니다. 코드 예제에서는 ASP.NET 멤버 자격 및 폼 인증 및 사용자가 만들어졌는지 해당 이름 및 암호를 알고 사용 하 여 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

다음 예제 코드에서는 앞의 예제 코드에 대 한 ChangePasswordMail.htm 파일로 사용할 수 있습니다.

중요

사용자 계정 이름 또는 전자 메일의 암호는 잠재적인 보안 위협을 보내는 중입니다. 전자 메일 메시지를 일반적으로 일반 텍스트로 보내고 애플리케이션 "스니핑" 하는 특수 한 네트워크에서 읽을 수 있습니다. 보안을 강화 하려면에 설명 된 완화 요소를 사용 하 여 보안 로그인 컨트롤합니다.

<html>  
<head><title></title></head>  
<body>  
<form>  

  <h1>Your password for the account named &quot;<%Username%>&quot; has changed.</h1>  

  <p>  
  If you did not initiate this change, please call 1-206-555-0100.  
  </p>  

  <p>  
  <a href="http://www.contoso.com/login.aspx">  
    <img src="cid:LoginGif" alt="Log In" />  
  </a>   
  </p>  

  <p>  
  Please read our attached Privacy Notice.  
  </p>  

</form>  
</body>  
</html>  

설명

OnSendMailError 메서드 SMTP 메일 시스템 사용자가 암호를 변경한 후 전자 메일 메시지 보내기를 시도 하는 동안 예외가 발생 하는 경우 호출 됩니다.

검사는 Exception 의 속성을 SendMailErrorEventArgs 변수로 전달 된 개체는 e 예외의 실제 원인을 확인 하려면 매개 변수. 가장 일반적인 원인은에 구성 오류가 smtp Web.config 파일의 섹션입니다.

설정 해야 합니다는 Handled 개체의 속성으로 전달 합니다 e 매개 변수를 true 예외의 care 수행 되었으면이 고 그렇지, 예외가 다시 throw 되는 신호를 합니다.

이벤트가 발생하면 대리자를 통해 이벤트 처리기가 호출됩니다. 자세한 내용은 ASP.NET Web Forms 페이지에서 서버 이벤트 처리합니다.

또한 OnSendMailError 메서드를 사용하면 파생 클래스가 대리자를 연결하지 않고도 이벤트를 처리할 수 있습니다. 이는 파생 클래스에서 이벤트를 처리하는 기본 방법입니다.

상속자 참고

재정의 하는 경우는 OnSendMailError(SendMailErrorEventArgs) 파생된 클래스에서 메서드를 호출 해야 합니다 OnSendMailError(SendMailErrorEventArgs) 대리자를 등록 하는 기본 클래스의 메서드는 이벤트를 받도록 합니다.

적용 대상

추가 정보