HOW TO:傳送健康監視通知的電子郵件

更新:2007 年 11 月

您可以設定 ASP.NET 應用程式在 ASP.NET 健康監視 Web 事件發生時傳送電子郵件通知。若要進行這項操作,您可以設定其中一個可用的電子郵件 Web 事件提供者。

ASP.NET 健康監視系統包含幾個使用健康監視 Web 事件資料的事件提供者 (Provider)。只有 EventLogWebEventProviderSqlWebEventProviderWmiWebEventProvider 事件提供者是在根 Web.config 檔中事先設定的。本主題描述如何加入電子郵件 Web 事件提供者的組態設定,以及如何讓那些提供者能夠接聽某些事件。

如需提供者可以接聽的 Web 事件清單,請參閱根 Web.config 檔中的 <eventMappings> 項目,或參閱 ASP.NET 健康監視事件概觀的「Web 事件」一節。

從 Web 應用程式傳送電子郵件

本主題中的程式碼範例需要設定您的 Web 應用程式傳送電子郵件訊息。下列程序向您顯示,如果您已在 Web 伺服器上安裝和設定 Simple Mail Transfer Protocol (SMTP) 服務,且能夠存取可傳遞電子郵件訊息的 SMTP 伺服器時,如何設定您的應用程式。如需詳細資訊,請參閱 HOW TO:在 IIS 6.0 中安裝和設定 SMTP 虛擬伺服器

若要設定 ASP.NET 應用程式傳送電子郵件

  • 開啟應用程式的 Web.config 檔,並設定 <system.net> 區段內的 <mailSettings> 項目。您需要在 <smtp> 項目的 deliveryMethod 屬性 (Attribute) 中指定傳遞方法,並在 <network> 項目的 host 屬性中指定 SMTP 伺服器的名稱。此外,建議您將 defaultCredentials 屬性設為 true (其他屬性可以使用,也會在項目相關的主題中進行說明)。

    您的組態設定可能看起來與下列範例相同。

    <configuration xmlns="https://schemas.microsoft.com/.NetConfiguration/v2.0">
      <system.net>
        <mailSettings>      <smtp deliveryMethod="Network">        <network           defaultCredentials="true"           host="smtpservername"           />      </smtp>    </mailSettings>
      </system.net>
      <!-- Other configuration settings. -->
    </configuration>
    

設定郵件提供者

若要設定 SimpleMailWebEventProvider Web 事件提供者

  1. 開啟應用程式的 Web.config 檔,並在 <system.web> 區段的 <healthMonitoring> 項目內加入新的 <providers> 項目。這個新的 providers 項目就是您將設定 SimpleMailWebEventProvider Web 事件提供者的地方。這個提供者包含在 ASP.NET 健康監視系統中,但未在根 Web.config 檔中為您事先設定。

    您的組態設定可能看起來與下列範例相同。

    <configuration>
      <system.web>
        <healthMonitoring enabled="true" heartbeatInterval="0">
          <providers>
            <add           name="exampleMailWebEventProvider"           type="System.Web.Management.SimpleMailWebEventProvider"          to="someone@contoso.com"          from="someone@contoso.com"          buffer="false"          subjectPrefix="WebEvent has fired"          />
          </providers>
        </healthMonitoring>
      </system.web>
      <!-- Other configuration settings. -->
    </configuration>
    

    為了這個程序的目的,請藉由將 buffer 屬性設為 false 停用事件緩衝。name 屬性是任意的,它用於在下一步中識別提供者。將 to 和 from 屬性變更為您自己的電子郵件地址,以完成在此程序中稍後的測試步驟。

  2. <healthMonitoring> 項目內加入新的 <rules> 項目。這個新的 rules 項目會設定電子郵件提供者接聽所有 Web 事件。

    您的組態設定可能看起來與下列範例相同。

    <configuration>
      <system.web>
        <healthMonitoring enabled="true" heartbeatInterval="0">
          <!-- <providers> element from the previous step -->
          <rules>
            <add           name="Testing Mail Event Providers"           eventName="All Events"           provider="exampleMailWebEventProvider"          profile="Default"           minInstances="1"           maxLimit="Infinite"           minInterval="00:01:00"          custom=""           />
          </rules>
        </healthMonitoring>
      </system.web>
      <!-- Other configuration settings. -->
    </configuration>
    

    provider 屬性與上一步中 providers 項目的 name 屬性相同。這個 rules 項目的 eventName 屬性與根 Web.config 檔中事先設定之 eventMappings 項目的名稱屬性相同。這個組態會為 ASP.NET 應用程式中發生的每一個 Web 事件傳送電子郵件訊息。這包括 (但是不僅限於) 應用程式啟動和停止、組態變更和要求。

    為了這個程序的目的,請在同一組態檔中定義之前的 rules 項目和 provider 項目。您的 provider 項目也可存在於組態檔之鍊結中高於根 Web.config 檔的組態檔中。

  3. 藉由要求應用程式中的一個頁面以測試組態。如果您已變更組態設定 (如此會引起應用程式重新啟動事件),則您可能已接收到電子郵件訊息。您接收的電子郵件可能具有類似於下列訊息範例的主旨和訊息主體。

    Subject: Event Notification 1, part 1: WebEvent has fired <event>
    Body: 
    

Application Information --------------- Application domain: /LM/w3svc/1/ROOT/ Trust level: Full Application Virtual Path: / Application Path: Machine name:

Events --------------- ---------------

若要設定 TemplatedMailWebEventProvider Web 事件提供者

  1. 開啟應用程式的 Web.config 檔,並在 <system.web> 區段的 <healthMonitoring> 項目內加入新的 <providers> 項目。這個新的 providers 項目就是您將設定 TemplatedMailWebEventProvider Web 事件提供者的地方。這個提供者包含在 ASP.NET 健康監視系統中,但未在根 Web.config 檔中為您事先設定。

    您的組態設定可能看起來與下列範例相同。

    <configuration>
      <system.web>
        <healthMonitoring enabled="true" heartbeatInterval="0">
          <providers>        <add           name="exampleTemplatedMailWebEventProvider"           type="System.Web.Management.TemplatedMailWebEventProvider"          template="template.aspx"           detailedTemplateErrors="true"          to="someone@contoso.com"          from="someone@contoso.com"          buffer="true"          bufferMode="Notification"          maxMessagesPerNotification="1"          maxEventsPerMessage="1"           />      </providers>
        </healthMonitoring>
      </system.web>
      <!-- Other configuration settings. -->
    </configuration>
    

    為了這個程序的目的,請藉由將 buffer 屬性設為 false 啟用事件緩衝。bufferMode 屬性值 Notification 是在根 Web.config 檔中事先定義的。如需緩衝的詳細資訊,請參閱擴充 ASP.NET 健康監視事件

    name 屬性是任意的,它用於在下一步中識別提供者。將 to 和 from 屬性變更為您自己的電子郵件地址,以完成在此程序中稍後的測試步驟。

  2. <healthMonitoring> 項目內加入新的 <rules> 項目。這個新的 rules 項目會設定電子郵件提供者接聽所有 Web 事件。

    您的組態設定可能看起來與下列範例相同。

    <configuration>
      <system.web>
        <healthMonitoring enabled="true" heartbeatInterval="0">
          <!-- <providers> element from the previous step -->
          <rules>
            <add           name="Testing Templated Mail Event Providers"           eventName="Request Processing Events"           provider="exampleTemplatedMailWebEventProvider"          profile="Default"           minInstances="1"           maxLimit="Infinite"           minInterval="00:00:00"          custom=""           />
          </rules>
        </healthMonitoring>
      </system.web>
      <!-- Other configuration settings. -->
    </configuration>
    

    provider 屬性與上一步中 providers 項目的 name 屬性相同。這個 rules 屬性的 eventName 屬性與根 Web.config 檔中事先設定之 eventMappings 項目的 name 屬性相同。這個組態會為 ASP.NET 應用程式中對網頁或其他資源所做的每一個要求傳送電子郵件訊息。

  3. 建立名為 Template.aspx 的電子郵件範本檔案,並將其置於應用程式的根目錄中。這個檔名是 providers 項目之 template 屬性的值。當應用程式中發生要求事件時,會使用字串運算式 (String Expression) 將資料填入範本檔案,並將範本檔案做為通知電子郵件訊息進行傳送。如需使用運算式的詳細資訊,請參閱 ASP.NET 運算式概觀

    您的範本檔案可能看起來與下列範例相同。

    <%@ Page Language="cs" %>
    <%@ Import Namespace="System.Web.Management" %>
    
    <script >
    
    void Page_Load() 
    {
        MailEventNotificationInfo info = TemplatedMailWebEventProvider.CurrentNotification;
        Label0.Text = "EventsDiscardedByBuffer: " + info.EventsDiscardedByBuffer + '\n';
        Label1.Text = "EventsInBuffer: " + info.EventsInBuffer + '\n';
        Label2.Text = "NotificationSequence: " + info.NotificationSequence + '\n';
        Label3.Text = "NotificationType: " + info.NotificationType + '\n';
        Label4.Text = "EventsInNotification: " + info.EventsInNotification + '\n';
        Label5.Text = "EventsRemaining: " + info.EventsRemaining + '\n';
        Label6.Text = "MessagesInNotification: " + info.MessagesInNotification + '\n';
        Label7.Text = "eventsDiscardedDueToMessageLimit: " + info.EventsDiscardedDueToMessageLimit + '\n';
        Label8.Text = "messageSequence: " + info.MessageSequence + '\n';
        Label9.Text = "LastNotificationUtc: " + info.LastNotificationUtc.ToLocalTime().ToString() + '\n';
    
        EventList.DataSource = info.Events;
        EventList.DataBind();
    }
    </script>
    
    <asp:Label  id="Label0" /><p />
    <asp:Label  id="Label1" /><p />
    <asp:Label  id="Label2" /><p />
    <asp:Label  id="Label3" /><p />
    <asp:Label  id="Label4" /><p />
    <asp:Label  id="Label5" /><p />
    <asp:Label  id="Label6" /><p />
    <asp:Label  id="Label7" /><p />
    <asp:Label  id="Label8" /><p />
    <asp:Label  id="Label9" /><p />
    
    <asp:DataList id="EventList" >
        <ItemTemplate>
          Event Received 
          Sequence: <%# DataBinder.Eval(Container.DataItem, "EventSequence") %><br>
          Details: <%# Container.DataItem.ToString() %>
        </ItemTemplate>
    </asp:DataList>
    
  4. 藉由要求應用程式中的一個頁面以測試組態。您應該馬上會接收到具有類似於下列訊息範例的主旨和訊息內文。

    Subject: Event Notification 1, part 1: <event>
    Body: 
    EventsDiscardedByBuffer: 0 
    EventsInBuffer: 0 
    NotificationSequence: 1 
    NotificationType: Flush 
    EventsInNotification: 1 
    EventsRemaining: 0 
    MessagesInNotification: 1 
    eventsDiscardedDueToMessageLimit: 0 
    messageSequence: 1 
    LastNotificationUtc: 1/1/0001 12:00:00 AM 
    Event Received Sequence: 1
    Details: 
    <event information>
    

請參閱

參考

healthMonitoring 項目 (ASP.NET 設定結構描述)

healthMonitoring 的 eventMappings 項目 (ASP.NET 設定結構描述)

healthMonitoring 的 providers 項目 (ASP.NET 設定結構描述)

healthMonitoring 的 rules 項目 (ASP.NET 設定結構描述)

<smtp> 項目 (網路設定)

其他資源

ASP.NET 健康監視事件概觀

緩衝 ASP.NET 健康監視事件

設定 ASP.NET 健康監視