MailDefinition Klasa

Definicja

Umożliwia kontrolce tworzenie wiadomości e-mail z plików tekstowych lub ciągów. Klasa ta nie może być dziedziczona.

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
Dziedziczenie
MailDefinition
Atrybuty
Implementuje

Przykłady

Poniższy przykład kodu tworzy internetową wiadomość e-mail ze strony Web Forms. Możesz wprowadzić tekst wiadomości w formularzu lub wprowadzić nazwę pliku tekstowego, który ma być używany jako treść wiadomości. Kod definiuje dwa zamiany ciągów dla wiadomości: lista adresatów z pola tekstowego Do formularza zastąpi ciąg "<%To%>", a tekst określony we From właściwości zastąpi ciąg "<%From%>".

Na stronie Web Forms, którą generuje ten kod, możesz kliknąć pozycję Utwórz wiadomość e-mail i wyświetlić tylko, aby utworzyć wiadomość e-mail i wyświetlić właściwości MailMessage obiektu na stronie sieci Web. Kliknij pozycję Utwórz wiadomość e-mail i wyślij , aby wyświetlić wiadomość e-mail na stronie sieci Web i wysłać wiadomość do adresatów przy użyciu internetowej poczty e-mail.

Ważne

Ta kontrolka ma pole tekstowe, które akceptuje dane wejściowe użytkownika, co jest potencjalnym zagrożeniem bezpieczeństwa. Domyślnie ASP.NET strony sieci Web sprawdzają, czy dane wejściowe użytkownika nie zawierają skryptów ani elementów HTML. Aby uzyskać więcej informacji, zobacz Script Exploits Overview (Omówienie luk w zabezpieczeniach skryptów).

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

Uwagi

Klasa MailDefinition może być używana przez kontrolki do tworzenia MailMessage obiektu na podstawie pliku tekstowego lub ciągu zawierającego treść wiadomości e-mail. MailDefinition Użyj klasy , aby uprościć tworzenie wstępnie zdefiniowanych wiadomości e-mail, które mają być wysyłane przez kontrolkę. Jeśli chcesz wysłać wiadomość e-mail, która nie korzysta z kontrolki, zobacz klasę System.Net.Mail .

Podstawianie tekstu w treści wiadomości e-mail można wprowadzić, przekazując do CreateMailMessage metody IDictionary wystąpienie mapujące ciągi na ich zamiany.

MailMessage Obiekt utworzony przez klasę MailDefinition jest wysyłany przy użyciu Send metody SmtpClient klasy . Aby móc wysyłać wiadomości e-mail, należy skonfigurować serwer poczty SMTP w pliku Web.config. Aby uzyskać więcej informacji, zobacz <smtp>, element (network Ustawienia).

Uwaga

Klasa MailDefinition nie obsługuje powiązania danych. MailDefinition Właściwości klasy nie mogą być powiązane z danymi przy użyciu <%# %> składni wyrażenia powiązania danych.

Konstruktory

MailDefinition()

Inicjuje nowe wystąpienie klasy MailDefinition.

Właściwości

BodyFileName

Pobiera lub ustawia nazwę pliku zawierającego tekst treści wiadomości e-mail.

CC

Pobiera lub ustawia rozdzielaną przecinkami listę adresów e-mail w celu wysłania kopii wiadomości (CC).

EmbeddedObjects

Pobiera kolekcję EmbeddedMailObject wystąpień, zwykle używaną do osadzania obrazów w MailDefinition obiekcie przed wysłaniem wiadomości e-mail do użytkownika.

From

Pobiera lub ustawia adres e-mail nadawcy wiadomości.

IsBodyHtml

Pobiera lub ustawia wartość wskazującą, czy treść wiadomości e-mail to HTML.

Priority

Pobiera lub ustawia priorytet wiadomości e-mail.

Subject

Pobiera lub ustawia wiersz tematu wiadomości e-mail.

Metody

CreateMailMessage(String, IDictionary, Control)

Tworzy wiadomość e-mail z pliku tekstowego do wysłania za pomocą protokołu SMTP (Simple Mail Transfer Protocol).

CreateMailMessage(String, IDictionary, String, Control)

Tworzy wiadomość e-mail z zamianami z pliku tekstowego do wysłania za pomocą protokołu SMTP (Simple Mail Transfer Protocol).

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

IStateManager.IsTrackingViewState

Pobiera wartość wskazującą, czy kontrolka serwera zapisuje zmiany w stanie widoku.

IStateManager.LoadViewState(Object)

Przywraca informacje o stanie widoku z poprzedniego żądania strony, które zostało zapisane przez metodę SaveViewState() .

IStateManager.SaveViewState()

Zapisuje wszelkie zmiany stanu widoku kontroli serwera, które wystąpiły od czasu opublikowania strony z powrotem na serwerze.

IStateManager.TrackViewState()

Powoduje śledzenie zmian stanu widoku w kontrolce serwera, dzięki czemu można je przechowywać w obiekcie kontrolki StateBag serwera.

Dotyczy

Zobacz też