<maildefinition>和&lt; %%&gt;占位符</maildefinition>

时间:2009-07-09 19:09:49

标签: c# asp.net asp.net-membership

......                


BodyFileName 属性引用包含邮件正文的磁盘文件。如果我们在正文文件( RegistrationMail.txt )中放置占位符<% UserName %><% Password %>,则 CreateUserWizard 会自动将这些占位符替换为用户名和已创建用户的密码。

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

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


感谢名单

1 个答案:

答案 0 :(得分:9)

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

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

创建自己的电子邮件机制并不困难。

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();
}
相关问题