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

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

Syntax
<BindableAttribute(False)> _
Public NotInheritable Class MailDefinition _
Implements IStateManager
[BindableAttribute(false)]
public sealed class MailDefinition : IStateManager
[BindableAttribute(false)]
public ref class MailDefinition sealed : IStateManager
[<Sealed>]
[<BindableAttribute(false)>]
type MailDefinition =
class
interface IStateManager
end
The MailDefinition type exposes the following members.

Constructors
|
| Name | Description |
|---|
.gif) | MailDefinition | Initializes a new instance of the MailDefinition class. | Top

Methods

Explicit Interface Implementations

Remarks
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). Note |
|---|
The MailDefinition class does not support data binding. Properties of the MailDefinition class cannot be bound to data using the <%# %> data-binding expression syntax. |

Examples
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. 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="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
Dim md As MailDefinition = New MailDefinition
md.BodyFileName = sourceMailFile.Text
md.CC = sourceCC.Text
md.From = sourceFrom.Text
md.Subject = sourceSubject.Text
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
Dim replacements As ListDictionary = New ListDictionary
replacements.Add("<%To%>", sourceTo.Text)
replacements.Add("<%From%>", sourceFrom.Text)
If useFile.Checked Then
Dim fileMsg As System.Net.Mail.MailMessage
fileMsg = md.CreateMailMessage(sourceTo.Text, replacements, Me)
Return fileMsg
Else
Dim textMsg As System.Net.Mail.MailMessage
textMsg = md.CreateMailMessage(sourceTo.Text, replacements, sourceBodyText.Text, Me)
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 >
<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> <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> </td>
</tr>
<tr>
<td align="right">From:</td>
<td style="WIDTH: 10px">
</td>
<td>
<asp:textbox id="sourceFrom" runat="server" columns="54">
</asp:textbox> <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> </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> </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> </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> </td>
</tr>
<tr>
<td style="WIDTH: 100px">
<asp:radiobutton id="useText" runat="server"
text="Enter text" width="80px" height="22px"
groupname="textSource">
</asp:radiobutton> </td>
<td style="WIDTH: 11px">
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</td>
<td> </td>
</tr>
<tr>
<td align="center" colspan="3">
<asp:textbox id="sourceBodyText" runat="server" columns="51"
textmode="MultiLine" rows="15">
</asp:textbox> </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> </p>
<p>
<asp:placeholder id="PlaceHolder1" runat="server">
</asp:placeholder> </p>
<p>
<asp:literal id="errorMsg" runat="server">
</asp:literal></p>
</form>
</body>
</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()
{
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 >
<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>
<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> </td>
</tr>
<tr>
<td align="right">From:</td>
<td style="WIDTH: 10px">
</td>
<td>
<asp:textbox id="sourceFrom" runat="server" columns="54">
</asp:textbox>
<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> </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> </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> </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> </td>
</tr>
<tr>
<td style="WIDTH: 100px">
<asp:radiobutton id="useText" runat="server"
text="Enter text" width="80px" height="22px"
groupname="textSource">
</asp:radiobutton> </td>
<td style="WIDTH: 11px">
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</td>
<td> </td>
</tr>
<tr>
<td align="center" colspan="3">
<asp:textbox id="sourceBodyText" runat="server" columns="51"
textmode="MultiLine" rows="15">
</asp:textbox> </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> </p>
<p>
<asp:placeholder id="PlaceHolder1" runat="server">
</asp:placeholder>
</p>
<p>
<asp:literal id="errorMsg" runat="server">
</asp:literal>
</p>
</form>
</body>
</html>

Version Information
.NET FrameworkSupported in: 4, 3.5, 3.0, 2.0

Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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

See Also
|
Bibliothèque de classes .NET Framework MailDefinition, classe Permet à un contrôle de créer des messages électroniques à partir de fichiers texte ou de chaînes. Cette classe ne peut pas être héritée.

Hiérarchie d'héritage
Espace de noms :
System.Web.UI.WebControls
Assembly :
System.Web (dans System.Web.dll)

Syntaxe
<BindableAttribute(False)> _
Public NotInheritable Class MailDefinition _
Implements IStateManager
[BindableAttribute(false)]
public sealed class MailDefinition : IStateManager
[BindableAttribute(false)]
public ref class MailDefinition sealed : IStateManager
[<Sealed>]
[<BindableAttribute(false)>]
type MailDefinition =
class
interface IStateManager
end
Le type MailDefinition expose les membres suivants.

Constructeurs

Méthodes

Implémentations d'interface explicite

Notes
La classe MailDefinition peut être utilisée par des contrôles pour créer un objet MailMessage à partir d'un fichier texte ou d'une chaîne qui contient le corps du message électronique. Utilisez la classe MailDefinition pour simplifier la création des messages électroniques prédéfinis à envoyer par un contrôle. Si vous souhaitez envoyer un message électronique sans utiliser de contrôle, consultez la classe System.Net.Mail. Vous pouvez faire des substitutions de texte dans le corps du message électronique en passant à la méthode CreateMailMessage une instance de IDictionary qui mappe des chaînes à leurs remplacements. L'objet MailMessage créé par la classe MailDefinition est envoyé à l'aide de la méthode Send de la classe SmtpClient. Pour pouvoir envoyer un message électronique, vous devez configurer un serveur de messagerie SMTP dans votre fichier Web.config. Pour plus d'informations, consultez <smtp>, élément (paramètres réseau). Remarque |
|---|
La classe MailDefinition ne prend pas en charge la liaison aux données. Les propriétés de la classe MailDefinition ne peuvent pas être liées aux données à l'aide de la syntaxe d'expression de liaison aux données <%# %>. |

Exemples
L'exemple de code suivant crée un message électronique Internet à partir d'une page Web Forms. Vous pouvez soit entrer le texte du message dans le formulaire, soit entrer le nom d'un fichier texte à utiliser comme corps du message. Le code définit deux remplacements de chaîne pour le message : la liste des destinataires à partir de la zone de texte À du formulaire remplacera la chaîne "<%À%>" et le texte spécifié dans la propriété From remplacera la chaîne "<%De%>". Sur la page Web Forms générée par ce code, vous pouvez cliquer sur Créer un message et afficher uniquement pour créer un message électronique et afficher les propriétés de l'objet MailMessage dans la page Web. Cliquez sur Créer un message et envoyer pour afficher le message électronique dans la page Web et envoyer le message aux destinataires à l'aide de la messagerie électronique Internet. Note de sécurité |
|---|
Ce contrôle a une zone de texte qui accepte l'entrée d'utilisateur, ce qui constitue une menace éventuelle pour la sécurité. Par défaut, les pages Web ASP.NET vérifient que l'entrée d'utilisateur n'inclut pas de script ni d'éléments HTML. Pour plus d'informations, consultez Vue d'ensemble des attaques de script. |
<%@ 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
Dim md As MailDefinition = New MailDefinition
md.BodyFileName = sourceMailFile.Text
md.CC = sourceCC.Text
md.From = sourceFrom.Text
md.Subject = sourceSubject.Text
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
Dim replacements As ListDictionary = New ListDictionary
replacements.Add("<%To%>", sourceTo.Text)
replacements.Add("<%From%>", sourceFrom.Text)
If useFile.Checked Then
Dim fileMsg As System.Net.Mail.MailMessage
fileMsg = md.CreateMailMessage(sourceTo.Text, replacements, Me)
Return fileMsg
Else
Dim textMsg As System.Net.Mail.MailMessage
textMsg = md.CreateMailMessage(sourceTo.Text, replacements, sourceBodyText.Text, Me)
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 >
<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> <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> </td>
</tr>
<tr>
<td align="right">From:</td>
<td style="WIDTH: 10px">
</td>
<td>
<asp:textbox id="sourceFrom" runat="server" columns="54">
</asp:textbox> <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> </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> </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> </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> </td>
</tr>
<tr>
<td style="WIDTH: 100px">
<asp:radiobutton id="useText" runat="server"
text="Enter text" width="80px" height="22px"
groupname="textSource">
</asp:radiobutton> </td>
<td style="WIDTH: 11px">
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</td>
<td> </td>
</tr>
<tr>
<td align="center" colspan="3">
<asp:textbox id="sourceBodyText" runat="server" columns="51"
textmode="MultiLine" rows="15">
</asp:textbox> </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> </p>
<p>
<asp:placeholder id="PlaceHolder1" runat="server">
</asp:placeholder> </p>
<p>
<asp:literal id="errorMsg" runat="server">
</asp:literal></p>
</form>
</body>
</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()
{
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 >
<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>
<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> </td>
</tr>
<tr>
<td align="right">From:</td>
<td style="WIDTH: 10px">
</td>
<td>
<asp:textbox id="sourceFrom" runat="server" columns="54">
</asp:textbox>
<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> </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> </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> </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> </td>
</tr>
<tr>
<td style="WIDTH: 100px">
<asp:radiobutton id="useText" runat="server"
text="Enter text" width="80px" height="22px"
groupname="textSource">
</asp:radiobutton> </td>
<td style="WIDTH: 11px">
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</td>
<td> </td>
</tr>
<tr>
<td align="center" colspan="3">
<asp:textbox id="sourceBodyText" runat="server" columns="51"
textmode="MultiLine" rows="15">
</asp:textbox> </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> </p>
<p>
<asp:placeholder id="PlaceHolder1" runat="server">
</asp:placeholder>
</p>
<p>
<asp:literal id="errorMsg" runat="server">
</asp:literal>
</p>
</form>
</body>
</html>

Informations de version
.NET FrameworkPris en charge dans : 4, 3.5, 3.0, 2.0

Plateformes
Windows 7, Windows Vista SP1 ou ultérieur, Windows XP SP3, Windows XP SP2 Édition x64, Windows Server 2008 (installation minimale non prise en charge), Windows Server 2008 R2 (installation minimale prise en charge avec SP1 ou version ultérieure), Windows Server 2003 SP2
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.

Sécurité des threads
Tous les membres static ( Shared en Visual Basic) publics de ce type sont thread-safe. Il n'est pas garanti que les membres d'instance soient thread-safe.

Voir aussi
|