共用方式為


MailDefinition 類別

定義

允許控制項從文字檔案或字串建立電子郵件訊息。 此類別無法獲得繼承。

public ref class MailDefinition sealed : System::Web::UI::IStateManager
[System.ComponentModel.Bindable(false)]
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.EmptyStringExpandableObjectConverter))]
public sealed class MailDefinition : System.Web.UI.IStateManager
[<System.ComponentModel.Bindable(false)>]
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.EmptyStringExpandableObjectConverter))>]
type MailDefinition = class
    interface IStateManager
Public NotInheritable Class MailDefinition
Implements IStateManager
繼承
MailDefinition
屬性
實作

範例

下列程式代碼範例會從 Web Forms 頁面建立因特網電子郵件訊息。 您可以在表單中輸入郵件的文字,或輸入要當做郵件本文的文字檔名稱。 程序代碼會定義郵件的兩個字串取代:表單 [收件者] 文本框中的收件者清單將會取代字串 %<To%”From<>>

在產生此程式碼的 [Web Forms] 頁面上,您可以按兩下 [ 建立電子郵件],並只顯示 以建立電子郵件訊息,並在網頁中顯示物件的屬性 MailMessage 。 按兩下 [建立電子郵件] 並傳送 至 ,即可在網頁中顯示電子郵件訊息,並使用因特網電子郵件將郵件傳送給收件者。

重要

此控件有一個文本框可接受使用者輸入,這是潛在的安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ page language="C#"%>
<%@ import namespace="System.Net.Mail"%>
<%@ import namespace="System.Reflection"%>
<%@ import namespace="System.Collections.Specialized"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    HtmlTable ShowMessage(System.Net.Mail.MailMessage msg)
    {
        HtmlTable table = new HtmlTable();
        HtmlTableRow topRow = new HtmlTableRow();
        HtmlTableCell fieldHeaderCell = new HtmlTableCell();
        HtmlTableCell valueHeaderCell = new HtmlTableCell();
        fieldHeaderCell.InnerText = "Field";
        topRow.Cells.Add(fieldHeaderCell);
        valueHeaderCell.InnerText = "Value";
        topRow.Cells.Add(valueHeaderCell);
        table.Rows.Add(topRow);
        
        foreach(PropertyInfo p in msg.GetType().GetProperties())
        {
            HtmlTableRow row = new HtmlTableRow();
            
            HtmlTableCell labelCell = new HtmlTableCell();
            
            HtmlTableCell valueCell = new HtmlTableCell();
 
            if (!((p.Name == "Headers") ||
                  (p.Name == "Fields")  ||
                  (p.Name == "Attachments")))
            {            
                labelCell.InnerText = String.Format("{0}",p.Name);
                row.Cells.Add(labelCell);

                valueCell.InnerText = String.Format("{0}",p.GetValue(msg,null));
                row.Cells.Add(valueCell);
            }
            
            table.Rows.Add(row);
        }
        
        return table;
    }
    
    System.Net.Mail.MailMessage CreateMessage()
    {
        // <Snippet2>
        MailDefinition md = new MailDefinition();
        // </Snippet2>
        // <Snippet3>
        md.BodyFileName = sourceMailFile.Text;
        // </Snippet3>
        // <Snippet4>
        md.CC = sourceCC.Text;
        // </Snippet4>
        // <Snippet5>
        md.From = sourceFrom.Text;
        // </Snippet5>
        // <Snippet6>
        md.Subject = sourceSubject.Text;
        // </Snippet6>
        // <Snippet10>
        if (sourcePriority.SelectedValue == "Normal")
        {
            md.Priority = MailPriority.Normal;
        }
        else if (sourcePriority.SelectedValue == "High")
        {
            md.Priority = MailPriority.High;
        }
        else if (sourcePriority.SelectedValue == "Low")
        {
            md.Priority = MailPriority.Low;
        }
        // </Snippet10>
        
        // <Snippet7>
        ListDictionary replacements = new ListDictionary();
        replacements.Add("<%To%>",sourceTo.Text);
        replacements.Add("<%From%>", md.From);
        // </Snippet7>
        if (true == useFile.Checked)
        { 
            // <Snippet8>
            System.Net.Mail.MailMessage fileMsg;
            fileMsg = md.CreateMailMessage(sourceTo.Text, replacements, this); 
            // </Snippet8>
            return fileMsg;
        } 
        else
        {
            // <Snippet9>
            System.Net.Mail.MailMessage textMsg;
            textMsg = md.CreateMailMessage(sourceTo.Text, replacements, sourceBodyText.Text, this);
            // </Snippet9>
            return textMsg;
        }
    }
    
    void createEMail_Click(object sender, System.EventArgs e)
    {
        System.Net.Mail.MailMessage msg = CreateMessage();
        
        PlaceHolder1.Controls.Add(ShowMessage(msg));          
    }
    
    void sendEMail_Click(object sender, System.EventArgs e)
    {
        System.Net.Mail.MailMessage msg = CreateMessage();
        
        PlaceHolder1.Controls.Add(ShowMessage(msg));          
        
        errorMsg.Text = String.Empty;
        try {
            SmtpClient sc = new SmtpClient();
            sc.Send(msg);
        }
        catch (HttpException ex) {
          errorMsg.Text = ex.ToString();
        }
    }
    
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Create an email message</title>
</head>
<body>
        <form id="Form1" runat="server">
            <table id="Table1" cellspacing="1" 
                style="padding:1; width:450px; text-align:center">
                <tr>
                    <td align="center" colspan="3">
                        <h3>Create an email message</h3>
                    </td>
                </tr>
                <tr>
                    <td align="right">To:</td>
                    <td style="WIDTH: 10px">
                    </td>
                    <td>
                        <asp:textbox id="sourceTo" runat="server" columns="54">
                        </asp:textbox>&nbsp;
                        <asp:requiredfieldvalidator id="RequiredFieldValidator1" 
                          runat="server" errormessage="*" controltovalidate="sourceTo">
                        </asp:requiredfieldvalidator>
                    </td>
                </tr>
                <tr>
                    <td align="right">Cc:</td>
                    <td style="WIDTH: 10px">
                    </td>
                    <td>
                        <asp:textbox id="sourceCC" runat="server" columns="54">
                        </asp:textbox>&nbsp;</td>
                </tr>
                <tr>
                    <td align="right">From:</td>
                    <td style="WIDTH: 10px">
                    </td>
                    <td>
                        <asp:textbox id="sourceFrom" runat="server" columns="54">
                        </asp:textbox>&nbsp;
                        <asp:requiredfieldvalidator id="RequiredFieldValidator2" 
                          runat="server" errormessage="*" controltovalidate="sourceFrom">
                        </asp:requiredfieldvalidator>
                    </td>
                </tr>
                <tr>
                    <td align="right">Subject:</td>
                    <td style="WIDTH: 10px">
                    </td>
                    <td>
                        <asp:textbox id="sourceSubject" runat="server" columns="54">
                        </asp:textbox>&nbsp;</td>
                </tr>
                <tr>
                    <td align="right">
                    Priority</td>
                    <td style="WIDTH: 10px">
                    </td>
                    <td>
                        <asp:dropdownlist id="sourcePriority" runat="server">
                            <asp:listitem value="Low">Low</asp:listitem>
                            <asp:listitem value="Normal" selected="true">Normal
                            </asp:listitem>
                            <asp:listitem value="High">High</asp:listitem>
                        </asp:dropdownlist>&nbsp;</td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td align="right">Source:</td>
                    <td style="WIDTH: 10px">
                    </td>
                    <td>
                        <table id="Table2" cellspacing="1" cellpadding="1" width="100%">
                            <tr>
                                <td style="WIDTH: 100px">
                                    <asp:radiobutton id="useFile" runat="server" text="Use file" 
                                      width="80px" groupname="textSource" checked="True">
                                    </asp:radiobutton>&nbsp;</td>
                                <td style="WIDTH: 11px">
                                </td>
                                <td>
                                    <p style="text-align:right">File name:</p>
                                </td>
                                <td>
                                    <asp:textbox id="sourceMailFile" runat="server" columns="22">
                                    mail.txt</asp:textbox>&nbsp;</td>
                            </tr>
                            <tr>
                                <td style="WIDTH: 100px">
                                    <asp:radiobutton id="useText" runat="server" 
                                        text="Enter text" width="80px" height="22px" 
                                        groupname="textSource">
                                    </asp:radiobutton>&nbsp;</td>
                                <td style="WIDTH: 11px">
                                </td>
                                <td>
                                </td>
                                <td>
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td align="center" colspan="3">
                        <asp:textbox id="sourceBodyText" runat="server" columns="51" 
                            textmode="MultiLine" rows="15">
                        </asp:textbox>&nbsp;</td>
                </tr>
                <tr>
                    <td align="center" colspan="3">
                        <asp:button id="createEMail" runat="server" 
                            text="Create email and display only"
                            onclick="createEMail_Click">
                        </asp:button>
                        <asp:button id="sendEMail" runat="server" 
                            text="Create email and send">
                        </asp:button></td>
                </tr>
            </table>
            <p>&nbsp;</p>
            <p>
                <asp:placeholder id="PlaceHolder1" runat="server">
                </asp:placeholder>&nbsp;
            </p>
            <p>
                <asp:literal id="errorMsg" runat="server">
                </asp:literal>
            </p>
        </form>
    </body>
</html>
<%@ page language="VB"%>
<%@ import namespace="System.Net.Mail"%>
<%@ import namespace="System.Reflection"%>
<%@ import namespace="System.Collections.Specialized"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">               
    Function ShowMessage(ByVal msg As System.Net.Mail.MailMessage) As HtmlTable
        Dim table As HtmlTable = New HtmlTable
        Dim topRow As HtmlTableRow = New HtmlTableRow
        Dim fieldHeaderCell As HtmlTableCell = New HtmlTableCell
        Dim valueHeaderCell As HtmlTableCell = New HtmlTableCell

        fieldHeaderCell.InnerText = "Field"
        topRow.Cells.Add(fieldHeaderCell)
        valueHeaderCell.InnerText = "Value"
        topRow.Cells.Add(valueHeaderCell)
        table.Rows.Add(topRow)

        Dim p As PropertyInfo
        For Each p In msg.GetType().GetProperties()
            Dim row As HtmlTableRow = New HtmlTableRow
            Dim labelCell As HtmlTableCell = New HtmlTableCell
            Dim valueCell As HtmlTableCell = New HtmlTableCell
        
            If (Not ((p.Name = "Headers") Or _
                   (p.Name = "Fields") Or _
                   (p.Name = "Attachments"))) Then
                labelCell.InnerText = String.Format("{0}", p.Name)
                row.Cells.Add(labelCell)
            
                valueCell.InnerText = String.Format("{0}", p.GetValue(msg, Nothing))
                row.Cells.Add(valueCell)
            End If
            table.Rows.Add(row)
        Next
        Return table
    End Function

    Function CreateMessage() As System.Net.Mail.MailMessage
        ' <Snippet2>
        Dim md As MailDefinition = New MailDefinition
        ' </Snippet2>

        ' <Snippet3>
        md.BodyFileName = sourceMailFile.Text
        ' </Snippet3>

        ' <Snippet4>
        md.CC = sourceCC.Text
        ' </Snippet4>

        ' <Snippet5>
        md.From = sourceFrom.Text
        ' </Snippet5>

        ' <Snippet6>
        md.Subject = sourceSubject.Text
        ' </Snippet6>

        ' <Snippet10>
        If sourcePriority.SelectedValue = "Normal" Then
            md.Priority = MailPriority.Normal
        ElseIf sourcePriority.SelectedValue = "High" Then
            md.Priority = MailPriority.High
        ElseIf sourcePriority.SelectedValue = "Low" Then
            md.Priority = MailPriority.Low
        End If
        ' </Snippet10>

        ' <Snippet7>
        Dim replacements As ListDictionary = New ListDictionary
        replacements.Add("<%To%>", sourceTo.Text)
        replacements.Add("<%From%>", sourceFrom.Text)
        ' </Snippet7>

        If useFile.Checked Then
            ' <Snippet8>
            Dim fileMsg As System.Net.Mail.MailMessage
            fileMsg = md.CreateMailMessage(sourceTo.Text, replacements, Me)
            ' </Snippet8>
            Return fileMsg
        Else
            ' <Snippet9>
            Dim textMsg As System.Net.Mail.MailMessage
            textMsg = md.CreateMailMessage(sourceTo.Text, replacements, sourceBodyText.Text, Me)
            ' </Snippet9>
            Return textMsg
        End If
    End Function

    Sub createEMail_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim msg As System.Net.Mail.MailMessage = CreateMessage()
        PlaceHolder1.Controls.Add(ShowMessage(msg))
    End Sub

    Sub sendEMail_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim msg As System.Net.Mail.MailMessage = CreateMessage()
        PlaceHolder1.Controls.Add(ShowMessage(msg))

        Try
            Dim sc As SmtpClient
            sc = New SmtpClient()
            sc.Send(msg)
        Catch ex As Exception
            errorMsg.Text = ex.ToString()
        End Try
    End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Create an email message</title>
</head>
<body>
        <form id="Form1" runat="server">
            <table id="Table1" cellspacing="1" 
                style="padding:1; width:450px; text-align:center">
                <tr>
                    <td align="center" colspan="3">
                        <h3>Create an email message</h3>
                    </td>
                </tr>
                <tr>
                    <td align="right">To:</td>
                    <td style="WIDTH: 10px">
                    </td>
                    <td>
                        <asp:textbox id="sourceTo" runat="server" columns="54">
                        </asp:textbox>&nbsp;<asp:requiredfieldvalidator 
                            id="RequiredFieldValidator1" runat="server" errormessage="*"
                            controltovalidate="sourceTo">
                        </asp:requiredfieldvalidator>
                    </td>
                </tr>
                <tr>
                    <td align="right">Cc:</td>
                    <td style="WIDTH: 10px">
                    </td>
                    <td>
                        <asp:textbox id="sourceCC" runat="server" columns="54">
                        </asp:textbox>&nbsp;</td>
                </tr>
                <tr>
                    <td align="right">From:</td>
                    <td style="WIDTH: 10px">
                    </td>
                    <td>
                        <asp:textbox id="sourceFrom" runat="server" columns="54">
                        </asp:textbox>&nbsp;<asp:requiredfieldvalidator 
                            id="RequiredFieldValidator2" runat="server" errormessage="*"
                            controltovalidate="sourceFrom">
                        </asp:requiredfieldvalidator>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                    Priority</td>
                    <td style="WIDTH: 10px">
                    </td>
                    <td>
                        <asp:dropdownlist id="sourcePriority" runat="server">
                            <asp:listitem value="Low">Low</asp:listitem>
                            <asp:listitem value="Normal" selected="true">Normal</asp:listitem>
                            <asp:listitem value="High">High</asp:listitem>
                        </asp:dropdownlist>&nbsp;</td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td align="right">Subject:</td>
                    <td style="WIDTH: 10px">
                    </td>
                    <td>
                        <asp:textbox id="sourceSubject" runat="server" columns="54">
                        </asp:textbox>&nbsp;</td>
                </tr>
                <tr>
                    <td align="right">Source:</td>
                    <td style="WIDTH: 10px">
                    </td>
                    <td>
                        <table id="Table2" cellspacing="1" cellpadding="1" width="100%">
                            <tr>
                                <td style="WIDTH: 100px">
                                    <asp:radiobutton id="useFile" runat="server" 
                                        text="Use file" width="80px" groupname="textSource"
                                        checked="True">
                                    </asp:radiobutton>&nbsp;</td>
                                <td style="WIDTH: 11px">
                                </td>
                                <td>
                                    <p style="text-align:right">File name:</p>
                                </td>
                                <td>
                                    <asp:textbox id="sourceMailFile" runat="server" columns="22">
                                    mail.txt</asp:textbox>&nbsp;</td>
                            </tr>
                            <tr>
                                <td style="WIDTH: 100px">
                                    <asp:radiobutton id="useText" runat="server" 
                                        text="Enter text" width="80px" height="22px"
                                        groupname="textSource">
                                    </asp:radiobutton>&nbsp;</td>
                                <td style="WIDTH: 11px">
                                </td>
                                <td>
                                </td>
                                <td>
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td align="center" colspan="3">
                        <asp:textbox id="sourceBodyText" runat="server" columns="51" 
                            textmode="MultiLine" rows="15">
                        </asp:textbox>&nbsp;</td>
                </tr>
                <tr>
                    <td align="center" colspan="3">
                        <asp:button id="createEMail" runat="server" 
                            text="Create email and display only" onclick="createEMail_Click">
                        </asp:button>
                        <asp:button id="sendEMail" runat="server" text="Create email and send">
                        </asp:button></td>
                </tr>
            </table>
            <p>&nbsp;</p>
            <p>
                <asp:placeholder id="PlaceHolder1" runat="server">
                </asp:placeholder>&nbsp;</p>
            <p>
                <asp:literal id="errorMsg" runat="server">
                </asp:literal></p>
        </form>
    </body>
</html>

備註

控件可以使用 類別 MailDefinition ,從文本檔或包含電子郵件訊息本文的字串建立 MailMessage 物件。 使用類別 MailDefinition 來簡化控件要傳送的預先定義電子郵件訊息。 如果您想要傳送電子郵件而不使用控件,請參閱 類別 System.Net.Mail

您可以傳遞至將字串對應至 CreateMailMessage 其取代的實例方法 IDictionary ,在電子郵件訊息本文中進行文字替代。

類別 MailMessageMailDefinition 建立的物件會使用 Send 類別的 SmtpClient 方法傳送。 若要能夠傳送電子郵件,您必須在 Web.config 檔案中設定 SMTP 郵件伺服器。 如需詳細資訊,請參閱 <smtp> 元素 (網路設定)

注意

類別 MailDefinition 不支持數據系結。 類別的屬性 MailDefinition 無法使用數據系結表達式語法系結至數據 <%# %>

建構函式

MailDefinition()

初始化 MailDefinition 類別的新執行個體。

屬性

BodyFileName

取得或設定包含電子郵件內文文字的檔案名稱。

CC

取得或設定電子郵件地址的逗號分隔清單,用於接收訊息的副本 (CC)。

EmbeddedObjects

取得 EmbeddedMailObject 執行個體的集合,通常用於在將電子郵件傳送至使用者之前,將影像嵌入 MailDefinition 物件。

From

取得或設定訊息寄件者的電子郵件地址。

IsBodyHtml

取得或設定值,指出電子郵件的主體是否為 HTML。

Priority

取得或設定電子郵件訊息的優先權。

Subject

取得或設定電子郵件訊息的主旨。

方法

CreateMailMessage(String, IDictionary, Control)

從文字檔建立要透過 SMTP (簡易郵件傳輸通訊協定) 傳送的電子郵件訊息。

CreateMailMessage(String, IDictionary, String, Control)

從文字檔使用取代文字建立要透過 SMTP (簡易郵件傳輸通訊協定) 傳送的電子郵件訊息。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

IStateManager.IsTrackingViewState

取得值,指出伺服器控制項是否正在儲存檢視狀態的變更。

IStateManager.LoadViewState(Object)

SaveViewState() 方法所儲存的先前頁面要求來還原檢視狀態資訊。

IStateManager.SaveViewState()

儲存自頁面回傳至伺服器以來所發生的任何伺服器控制項檢視狀態變更。

IStateManager.TrackViewState()

導致對伺服器控制項的檢視狀態變更的追蹤 (Tracking),以便它們能夠儲存於伺服器控制項的 StateBag 物件。

適用於

另請參閱