2009-07-09 9 views
5

... <MailDefinition>と<%%>プレースホルダ


BodyFileName属性は、メールの本文を含むディスク・ファイルを参照します。本体テキストファイル(RegistrationMail.txt)にプレースホルダー<% UserName %><% Password %>を入れると、CreateUserWizardは、作成されたユーザーのユーザー名とパスワードでこれらのプレースホルダーを自動的に置き換えます。

A)プレースホルダ<% %>をあるテキストのファイルに置き換えることができるコントロールを作成するにはどうすればよいですか?

B)コードビハインドファイルからこれらのプレースホルダに書き込むことはできますか?つまり、呼び出されたときに、特定のテキストをtxtファイルの中に含まれるプレースホルダに書き込むメソッドがありますか? SendingMailイベントで呼び出さ


ありがとう

答えて

9

シンプル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("[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(); 
} 
関連する問題