This documentation is archived and is not being maintained.

MailDefinition.CreateMailMessage Method (String, IDictionary, Control)

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

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

public MailMessage CreateMailMessage(
	string recipients,
	IDictionary replacements,
	Control owner


Type: System.String
A comma-separated list of message recipients.
Type: System.Collections.IDictionary
An IDictionary containing a list of strings and their replacement strings.
Type: System.Web.UI.Control
The Control that owns this MailDefinition.

Return Value

Type: System.Net.Mail.MailMessage
A new MailMessage object.


replacements does not contain strings.


The From value in the SMTP section of the configuration file is null or the empty string

- or -

recipients contains an incorrect e-mail address.


owner is null.

The CreateMailMessage method creates a new MailMessage object that can be sent using the SmtpClient.Send method.

The BodyFormat property must be set to indicate whether the mail message should be formatted as plain text (MailFormat.Text) or HTML (MailFormat.Html).

The recipients parameter contains a comma-separated list of recipients of the e-mail message. If the recipients parameter contains an improperly formatted Internet e-mail address, the Send method throws an HttpException exception and the e-mail message is not sent.

The replacements parameter is an IDictionary instance that contains a list of strings to substitute. Strings are replaced in the order in which they were added to the IDictionary collection, and they can overwrite earlier replacements.

The owner parameter indicates which control is the parent of the MailDefinition control. It determines which directory to search for the text file specified in the BodyFileName property.

The following code example creates a ListDictionary object that defines two strings ("<%To%>" and "<%From%>") that are replaced in the e-mail message.

This code example is part of a larger example provided for the MailDefinition class.

ListDictionary replacements = new ListDictionary();
replacements.Add("<%From%>", md.From);

The following code example uses the CreateMailMessage method to create a new e-mail message from a text file.

This code example is part of a larger example provided for the MailDefinition class.

System.Net.Mail.MailMessage fileMsg;
fileMsg = md.CreateMailMessage(sourceTo.Text, replacements, this); 

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), 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.