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 窗体页创建 Internet 电子邮件。 可以在窗体中输入邮件的文本,也可以输入要用作邮件正文的文本文件的名称。 该代码定义邮件的两个字符串替换:窗体的“收件人”文本框中的收件人列表将替换字符串“<%To%>”,属性中指定的 From 文本将替换字符串“<%From%>”。

在此代码生成的 Web 窗体页上,可以单击“ 创建电子邮件并仅显示 ”以创建电子邮件并在网页中显示 对象的属性 MailMessage 。 单击“ 创建电子邮件并发送 ”,在网页中显示电子邮件,并使用 Internet 电子邮件将邮件发送给收件人。

重要

此控件有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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 将字符串映射到其替换项的实例,在电子邮件正文中替换文本。

MailMessage 创建 MailDefinition 的对象是使用 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()

导致跟踪服务器控件的视图状态的更改,以便这些更改可以存储到服务器控件的 StateBag 对象中。

适用于

另请参阅