EmbeddedMailObject 類別

定義

表示嵌入使用 MailDefinition 類別所建構之電子郵件訊息的項目。Represents an item to embed in an email message constructed using the MailDefinition class.

public ref class EmbeddedMailObject sealed
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.EmbeddedMailObject/EmbeddedMailObjectTypeConverter))]
public sealed class EmbeddedMailObject
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.EmbeddedMailObject/EmbeddedMailObjectTypeConverter))>]
type EmbeddedMailObject = class
Public NotInheritable Class EmbeddedMailObject
繼承
EmbeddedMailObject
屬性

範例

下列程式碼範例示範使用 Web 控制項的 ASP.NET 網頁 ChangePassword ,並包含名為之事件的事件處理常式 SendingMail SendingMailThe following code example demonstrates an ASP.NET page that uses a ChangePassword Web control, and includes an event handler for the SendingMail event named SendingMail. 此程式碼範例假設 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物件的屬性 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

下列範例程式碼可用來做為上述範例程式碼的 ChangePasswordMail.htm 檔案。The following example code can be used as the ChangePasswordMail.htm file for the preceding example code.

重要

在電子郵件中傳送使用者帳戶名稱或密碼是潛在的安全性威脅。Sending user account names or passwords in email is a potential security threat. 電子郵件訊息通常會以純文字傳送,並可由特殊的網路「探查」應用程式來讀取。Email messages are typically sent in plain text and can be read by special network "sniffing" applications. 若要改善安全性,請使用 保護登入控制項中所述的緩和措施。To improve security, use the mitigations that are described in Securing Login Controls.

<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>  

備註

EmbeddedMailObject代表要在郵件訊息中內嵌的專案。The EmbeddedMailObject represents an item to embed in a mail message. 這些內嵌專案可以是影像檔案,例如公司標誌。These embedded items can be image files such as company logos. 每個內嵌專案都是由識別碼和路徑指定。Each embedded item is specified by an identifier and a path.

若要確保内嵌物件在電子郵件訊息檔案中正確顯示,必須符合下列條件:To ensure that an embedded object is displayed correctly within the email message file, the following conditions must be met:

  • 郵件訊息為 HTML 格式。The mail message is in HTML format.

  • 專案是影像檔案 ( .jpg、.gif、.bmp 等等,) 。The item is an image file (.jpg, .gif, .bmp, and so on).

  • 在屬性中指定的 HTML 格式的內文檔案, BodyFileName 包含使用下列語法的影像檔參考:The HTML-formatted body file specified in the BodyFileName property contains a reference to the image file using the following syntax:

    <img src="cid:identifier" alt="Alternate Text" />.  
    

如果 EmbeddedMailObject 新增至郵件訊息,但未滿足先前指定的所有需求,則很可能會在郵件訊息中顯示為附件。If an EmbeddedMailObject is added to a mail message and does not fulfill all of the requirements specified previously, it will most likely be displayed as an attachment in the mail message. 如果專案是由郵件中的識別碼所參考,但未包含在內嵌專案中,則會在郵件被查看時顯示為中斷的附件。If an item is referenced by an identifier in the mail message but not included as an embedded item, it will appear as a broken attachment when the mail is viewed.

EmbeddedMailObjectsCollection 儲存 EmbeddedMailObject 單一郵件訊息的物件集合。The EmbeddedMailObjectsCollection stores a collection of EmbeddedMailObject objects for a single mail message. EmbeddedMailObjectsCollection物件的屬性會使用 EmbeddedObjects MailDefinition 來建立郵件訊息。The EmbeddedMailObjectsCollection is used by the EmbeddedObjects property of the MailDefinition object to create the mail message.

允許内嵌物件在下列 Web 控制項中設定的郵件訊息,可以透過宣告 MailDefinition 方式設定其屬性:Mail messages that allow embedded objects are configurable in the following Web controls by setting their MailDefinition properties declaratively:

注意

和物件中的 EmbeddedMailObjectEmbeddedMailObjectsCollection 不會儲存在 view 狀態中。The values in the EmbeddedMailObject and EmbeddedMailObjectsCollection objects are not stored in view state. 這可防止惡意使用者探索您伺服器的路徑資訊。This protects against malicious users discovering path information for your server.

建構函式

EmbeddedMailObject()

初始化 EmbeddedMailObject 類別的新執行個體。Initializes a new instance of the EmbeddedMailObject class.

EmbeddedMailObject(String, String)

初始化 EmbeddedMailObject 類別的新執行個體,並使用指定的識別項名稱及路徑填入物件。Initializes a new instance of the EmbeddedMailObject class, using the specified identifier name and path to populate the object.

屬性

Name

取得或設定要用為嵌入在以 MailDefinition 類別建構之郵件訊息中的項目識別項名稱。Gets or sets the name that is used as the identifier of the item to be embedded in a mail message constructed with the MailDefinition class.

Path

取得或設定用來擷取要嵌入以 MailDefinition 類別建構之郵件訊息的項目路徑。Gets or sets the path that is used to retrieve an item to embed in a mail message constructed with the MailDefinition class.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets 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)

適用於

另請參閱