Uredi

Deli z drugimi prek


ChangePassword.MailDefinition Property

Definition

Gets a reference to a collection of properties that define the email message that is sent to users after they have changed their password.

public:
 property System::Web::UI::WebControls::MailDefinition ^ MailDefinition { System::Web::UI::WebControls::MailDefinition ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
[System.Web.UI.Themeable(false)]
public System.Web.UI.WebControls.MailDefinition MailDefinition { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
[<System.Web.UI.Themeable(false)>]
member this.MailDefinition : System.Web.UI.WebControls.MailDefinition
Public ReadOnly Property MailDefinition As MailDefinition

Property Value

A reference to a MailDefinition object that defines the email message sent to a new user.

Attributes

Exceptions

The From property is not set to an email address.

Examples

The following code example shows how to use the MailDefinition property to define an email message that is sent to users who change their passwords. It assumes that there is a file called MailFile.txt that contains the text of the email message to send.

To be able to send email messages to users, you must configure an email server in the Web.config file for your application. For more information, see How to: Install and Configure SMTP Virtual Servers in IIS 6.0.

<%@ page language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  void Changepassword1_SendingMail(object sender, MailMessageEventArgs e)
  {
    // Set mail message fields.
    e.Message.Subject = "New user on Web site.";
    // Replace placeholder text in message body with
    // information provided by the user.
    e.Message.Body = e.Message.Body.Replace("<%ChangedDate%>", (DateTime.Now.ToString()) );
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:changepassword id="Changepassword1" runat="server" 
        maildefinition-bodyfilename="~/MailFiles/mailfile.txt" 
          maildefinition-from="userAdmin@your.site.name.here" 
          onsendingmail="Changepassword1_SendingMail">
      </asp:changepassword>
    </div>
    </form>
</body>
</html>
<%@ page language="VB"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  Sub Changepassword1_SendingMail(ByVal sender As Object, ByVal e As MailMessageEventArgs)
        ' Set mail message fields.
        e.Message.Subject = "New user on Web site."
        ' Replace placeholder text in message body with information 
        '  provided by the user. 
        e.Message.Body = e.Message.Body.Replace("<%ChangedDate%>", DateTime.Now.ToString())
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:changepassword id="Changepassword1" runat="server" 
          maildefinition-bodyfilename="~/MailFiles/mailfile.txt"
          maildefinition-from="userAdmin@your.site.name.here" 
          onsendingmail="Changepassword1_SendingMail">
      </asp:changepassword>
    </div>
    </form>
</body>
</html>

Remarks

The MailDefinition property returns a reference to a group of properties that you use to define the format and content of the email message sent to users after they have changed their password. Common settings include the subject line and the sender's return address. For a complete list of properties, see MailDefinition.

The MailDefinition property is read-only; however, you can set the properties of the MailDefinition object it returns. You can set these properties in the form property-subproperty, where subproperty represents a property of the MailDefinition class (for example, MailDefinition-Subject). You can also set the properties programmatically in the form Property.Subproperty (for example,MailDefinition.Subject).

An email message is sent only if the user has an email address registered with the membership provider and if the BodyFileName property of the MailDefinition property points to a valid file. If the BodyFileName property is set, the From property must be set to an email address; otherwise, an HttpException exception is thrown.

If the email message is created from the MailDefinition object, it will make the substitutions shown in the following table. The substitution text is case-insensitive.

Substitution text Replaced with
<% UserName %> The Web site user name of the user.
<% Password %> The new password for the user.

If the IsBodyHtml property of the MailDefinition object is true, the contents of the mail message will be HTML-encoded to guard against cross-site scripting security vulnerabilities for the message recipient.

You can use the SendingMail event to modify the MailMessage object that is created by the MailDefinition object.

Important

Sending user account names or passwords in email is a potential security threat. Email messages are typically sent in plain text and can be read by special network "sniffing" applications. To improve security, use the mitigations that are described in Securing Login Controls.

This property cannot be set by themes or style sheet themes. For more information, see ThemeableAttribute and ASP.NET Themes and Skins.

Applies to

See also