我试图在我的C#Web应用程序中通过PasswordRecovery控件生成的电子邮件中包含一个附加变量。我有一个User_Profile表,其中包含用户的名字和姓氏,但我不知道如何通过变量使用将该信息传递给HTML电子邮件模板。 <%Username%>
和<%Password%>
工作正常,但在尝试传递firstName时,我得到的只是占位符。这就是我所拥有的:
ASPX:
<asp:HiddenField ID="hfFirstName" runat="server" />
<asp:PasswordRecovery ID="ResetPassword" runat="server" ClientIDMode="Static" OnSendingMail="ResetPassword_SendingMail">
<MailDefinition BodyFileName="~/EmailTemplates/HTMLEmailPWReset.html" IsBodyHtml="true" Subject="Your password has been reset" />
<UserNameTemplate>
<h2>Reset my password</h2>
<p>Forgot your password? Enter your User Name below.</p>
<div class="form-group">
<asp:TextBox ID="UserName" CssClass="form-control" placeholder="User Name" runat="server" />
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="User Name is required" ValidationGroup="ResetPassword" Display="Dynamic" />
<asp:Label ID="FailureText" runat="server" CssClass="field-validation-error" EnableViewState="False" />
</div>
<div class="row">
<div class="col-md-6">
<asp:Button ID="SubmitButton" runat="server" CommandName="Submit" Text="Submit" CssClass="btn btn-primary btn-block" ValidationGroup="ResetPassword" />
</div>
<div class="col-md-6">
<asp:Button ID="cancel" CssClass="btn btn-default btn-block" runat="server" Text="Cancel" OnClick="cancel_Click" CausesValidation="false" />
</div>
</div>
</UserNameTemplate>
<QuestionTemplate>
<h2>Reset my password</h2>
<h4>Identity Confirmation</h4>
<p>Please answer the following security question to receive your new password.</p>
<div class="form-group">
<label><strong>User Name:</strong>
</label>
<asp:label ID="UserName" CssClass="form-control form-control-static" runat="server" />
</div>
<div class="form-group">
<label><strong>Question:</strong>
</label>
<asp:label ID="Question" CssClass="form-control" runat="server" />
</div>
<div class="form-group">
<label><strong>Answer:</strong>
</label>
<asp:textbox ID="Answer" runat="server" CssClass="form-control" />
<asp:RequiredFieldValidator ID="AnswerRequired" ControlToValidate="Answer" ErrorMessage="Answer is required" runat="server" CssClass="field-validation-error" Display="Dynamic" />
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<asp:Button ID="SubmitButton" runat="server" CommandName="Submit" Text="Submit" CssClass="btn btn-primary btn-block" ValidationGroup="ResetPassword" />
</div>
<div class="col-md-6">
<asp:Button ID="cancel" CssClass="btn btn-default btn-block" runat="server" Text="Cancel" OnClick="cancel_Click" CausesValidation="false" />
</div>
</div>
</div>
<asp:Label ID="FailureText" runat="server" CssClass="field-validation-error" EnableViewState="False" />
</QuestionTemplate>
<SuccessTemplate>
<h2>Reset my password</h2>
<div class="form-group">
<p>Your new password has been sent to your registration email address.</p>
<p>
<asp:Button ID="continue" CssClass="btn btn-primary btn-block" runat="server" Text="Continue" OnClick="cancel_Click" CausesValidation="false" />
</p>
</div>
</SuccessTemplate>
</asp:PasswordRecovery>
HTML电子邮件模板:
<body class="auto-style2">
<p>Hi <%FirstName%>.</p>
<p>According to our records... Your new password is: <strong><%Password%></strong></p>
<p>Thank you.</p>
</body>
C#:
protected void ResetPassword_SendingMail(object sender, MailMessageEventArgs e)
{
GetUserProfile();
string firstName = hfFirstName.Value;
e.Message.Body.Replace("<%FirstName%>", firstName);
}
private void GetUserProfile()
{
string userName = (Convert.ToString(((TextBox)ResetPassword.Controls[0].FindControl("UserName")).Text));
using (SqlConnection con = new SqlConnection(sqlConString))
{
try
{
SqlCommand cmd = new SqlCommand(
@"SELECT *
FROM [dbo].[User_Profile] UP
INNER JOIN [Users] U
ON U.[UserId] = UP.[UserId]
WHERE U.[UserName] = @userName");
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@userName", userName));
con.Open();
SqlDataReader PopulateReader;
PopulateReader = cmd.ExecuteReader();
while (PopulateReader.Read())
{
hfFirstName.Value = (PopulateReader["FirstName"].ToString());
}
}
catch (Exception ex)
{
log.InfoFormat("Exception caught in GetUserProfile(): {0}", ex.ToString());
log.Info(Environment.NewLine + "End Exception" + Environment.NewLine);
}
}
}
我意识到GetUserProfile()/ HiddenField方法可能不是最好的,但它存储并检索&#39; firstName&#39;的正确值。变量。看起来故障发生在e.Message.Body.Replace("<%FirstName%>", firstName);
,但我没有收到任何错误,我无法确定占位符没有被替换的原因。我在这里做错了什么想法?
提前感谢您的光临,任何帮助都会受到最高的评价。
答案 0 :(得分:0)
在这里找到我的答案:String is not replaced in CreateUserWizard Registration Email。 e.Message.Body.Replace("<%FirstName%>", firstName);
不正确。正确的方法是e.Message.Body = e.Message.Body.Replace("<%FirstName%>", firstName.ToString());
。现在,占位符已正确替换为所需的值。感谢。