Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<MailDefinition> and <%%> placeholders

...


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

like image 452
SourceC Avatar asked Jul 09 '09 19:07

SourceC


1 Answers

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();
}
like image 183
Greg Avatar answered Oct 13 '22 22:10

Greg