views:

605

answers:

1

Hello,

<asp:CreateUserWizard> ...
   <MailDefinition 
       BodyFileName="~/RegistrationMail.txt" 
       From="[email protected]" 
       Subject="new account">
   </MailDefinition>


BodyFileName attribute references disk file containing mail's body text. If we put placeholders <% UserName %> and <% Password %> in the body text file ( RegistrationMail.txt ), then CreateUserWizard will automatically replace these placeholders with username and password of a created user.

A) If I wanted to create a control that would also be able to replace placeholders <% %> in a file with some text, how would I do that?

B) Can I also write into these placeholders from code behind file? Meaning, is there some method which when called, writes specific text into placeholder contained inside some txt file?


thanx

+1  A: 

A simple string.Replace() called in the SendingMail event does the trick.

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

Creating your own emailing mechanism isn't that difficult either.

using( MailMessage message = new MailMessage() )
{
    message.To.Add( "[email protected]" );
    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();
}
Greg