ChangePassword.SendMailError 이벤트

정의

이메일 메시지를 사용자에게 보내는 과정에서 SMTP 오류가 있을 때 발생합니다.

public:
 event System::Web::UI::WebControls::SendMailErrorEventHandler ^ SendMailError;
public event System.Web.UI.WebControls.SendMailErrorEventHandler SendMailError;
member this.SendMailError : System.Web.UI.WebControls.SendMailErrorEventHandler 
Public Custom Event SendMailError As SendMailErrorEventHandler 

이벤트 유형

예제

다음 코드 예제를 사용 하는 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>  

설명

SendMailError SMTP 메일 공급자는 사용자가 자신의 암호를 변경한 후 전자 메일 메시지를 보내려고 할 때 예외를 throw 하면 이벤트가 발생 합니다. 이 이벤트는 발생 하는 가장 일반적인 이유는는 smtp Web.config 파일의 섹션에 올바르지 않습니다. 섹션에 smtp 대한 자세한 내용은 smtp> 요소(네트워크 설정)를 참조<하세요.

기본 SendMailError 이벤트 처리기를 catch 하거나 메일 시스템에서 SMTP 오류를 처리 하지 않습니다. 프로그램 SendMailError 이벤트 처리기를 설정 해야 합니다는 Handled 의 속성을 SendMailErrorEventArgs 개체를 true 오류가 사용자에 게 표시 되지 않도록 중지 하려면.

이벤트 처리에 대한 자세한 내용은 이벤트 처리 및 발생 을 참조하십시오.

적용 대상

추가 정보