<MailDefinition>和<%%>占位符

Sou*_*ceC 5 c# asp.net asp.net-membership

...


BodyFileName属性引用包含邮件正文的磁盘文件.如果我们把占位符<% UserName %>,并<% Password %>在正文文件(RegistrationMail.txt),然后CreateUserWizard控件会自动替换这些占位符与创建的用户的用户名和密码.

A)如果我想创建一个控件,它也能够<% %>用一些文本替换文件中的占位符,我该怎么做?

B)我还可以从文件后面的代码中写入这些占位符吗?意思是,是否有一些方法在调用时,将特定文本写入包含在某个txt文件中的占位符?


感谢名单

Gre*_*reg 9

在SendingMail事件中调用的简单string.place()可以解决这个问题.

protected void CreateUserWizard1_SendingMail( object sender, MailMessageEventArgs e )
{
    // Replace <%foo%> placeholder with foo value
    e.Message.Body = e.Message.Body.Replace( "<%foo%>", foo );
}
Run Code Online (Sandbox Code Playgroud)

创建自己的电子邮件机制也不是那么困难.

using( MailMessage message = new MailMessage() )
{
    message.To.Add( "none@none.com" );
    message.Subject = "Here's your new password";
    message.IsBodyHtml = true;
    message.Body = GetEmailTemplate();

    // Replace placeholders in template.
    message.Body = message.Body.Replace( "<%Password%>", newPassword );
    message.Body = message.Body.Replace( "<%LoginUrl%>", HttpContext.Current.Request.Url.GetLeftPart( UriPartial.Authority ) + FormsAuthentication.LoginUrl ); // Get the login url without hardcoding it.

    new SmtpClient().Send( message );
}

private string GetEmailTemplate()
{
    string templatePath = Server.MapPath( @"C:\template.rtf" );

    using( StreamReader sr = new StreamReader( templatePath ) )
        return sr.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)