MailDefinition Class

 

Allows a control to create e-mail messages from text files or strings. This class cannot be inherited.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

System.Object
  System.Web.UI.WebControls.MailDefinition

[BindableAttribute(false)]
public sealed class MailDefinition : IStateManager

NameDescription
System_CAPS_pubmethodMailDefinition()

Initializes a new instance of the MailDefinition class.

NameDescription
System_CAPS_pubpropertyBodyFileName

Gets or sets the name of the file that contains text for the body of the e-mail message.

System_CAPS_pubpropertyCC

Gets or sets a comma-separated list of e-mail addresses to send a copy (CC) of the message to.

System_CAPS_pubpropertyEmbeddedObjects

Gets a collection of EmbeddedMailObject instances, typically used to embed images in a MailDefinition object before sending an e-mail to a user.

System_CAPS_pubpropertyFrom

Gets or sets the e-mail address of the message sender.

System_CAPS_pubpropertyIsBodyHtml

Gets or sets a value indicating whether the body of the e-mail is HTML.

System_CAPS_pubpropertyPriority

Gets or sets the priority of the e-mail message.

System_CAPS_pubpropertySubject

Gets or sets the subject line of the e-mail message.

NameDescription
System_CAPS_pubmethodCreateMailMessage(String, IDictionary, Control)

Creates an e-mail message from a text file to send by means of SMTP (Simple Mail Transfer Protocol).

System_CAPS_pubmethodCreateMailMessage(String, IDictionary, String, Control)

Creates an e-mail message with replacements from a text file to send by means of SMTP (Simple Mail Transfer Protocol).

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateManager.LoadViewState(Object)

Restores view-state information from a previous page request that was saved by the SaveViewState method.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateManager.SaveViewState()

Saves any server control view-state changes that have occurred since the time the page was posted back to the server.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateManager.TrackViewState()

Causes tracking of view-state changes to the server control so they can be stored in the server control's StateBag object.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIStateManager.IsTrackingViewState

Gets a value that indicates whether the server control is saving changes to its view state.

The MailDefinition class can be used by controls to create a MailMessage object from a text file or a string that contains the body of the e-mail message. Use the MailDefinition class to simplify creating predefined e-mail messages to be sent by a control. If you want to send e-mail not using a control, see the System.Net.Mail class.

You can make text substitutions in the body of the e-mail message by passing to the CreateMailMessage method an IDictionary instance that maps strings to their replacements.

The MailMessage object created by the MailDefinition class is sent using the Send method of the SmtpClient class. To be able to send e-mail, you must configure an SMTP mail server in your Web.config file. For more information, see the <smtp> Element (Network Settings).

System_CAPS_noteNote

The MailDefinition class does not support data binding. Properties of the MailDefinition class cannot be bound to data using the <%#   %> data-binding expression syntax.

The following code example creates an Internet e-mail message from a Web Forms page. You can either enter the text of the message in the form or enter the name of a text file to use as the body of the mail. The code defines two string replacements for the message: the recipient list from the form's To text box will replace the string "<%To%>", and the text specified in the From property will replace the string "<%From%>".

On the Web Forms page that this code generates, you can click Create e-mail and display only to create an e-mail message and display the properties of the MailMessage object in the Web page. Click Create e-mail and send to both display the e-mail message in the Web page and send the message to the recipients using Internet e-mail.

System_CAPS_security Security Note

This control has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see 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()
    {
        MailDefinition md = new MailDefinition();
        md.BodyFileName = sourceMailFile.Text;
        md.CC = sourceCC.Text;
        md.From = sourceFrom.Text;
        md.Subject = sourceSubject.Text;
        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;
        }

        ListDictionary replacements = new ListDictionary();
        replacements.Add("<%To%>",sourceTo.Text);
        replacements.Add("<%From%>", md.From);
        if (true == useFile.Checked)
        { 
            System.Net.Mail.MailMessage fileMsg;
            fileMsg = md.CreateMailMessage(sourceTo.Text, replacements, this); 
            return fileMsg;
        } 
        else
        {
            System.Net.Mail.MailMessage textMsg;
            textMsg = md.CreateMailMessage(sourceTo.Text, replacements, sourceBodyText.Text, this);
            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 e-mail 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 e-mail 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 e-mail and display only"
                            onclick="createEMail_Click">
                        </asp:button>
                        <asp:button id="sendEMail" runat="server" 
                            text="Create e-mail 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>

.NET Framework
Available since 2.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: