【发布时间】:2019-09-17 21:33:04
【问题描述】:
在我的 ASP.NET MVC Web 应用程序中,我设置了密码恢复并且一切正常,但是超链接无法正确显示。
它会打印出整个 url 而不是我想要的文本,例如。 “这里”
这是我的代码的问题还是只是电子邮件的问题。
string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Holiday Tracker Reset Password", "Please reset your password by using the following link. Copy and paste it into the url. <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("ForgotPasswordConfirmation", "Account");
Sned 电子邮件操作:
void sendMail(IdentityMessage message)
{
#region formatter
string text = string.Format("", message.Subject, message.Body);
string html = "";
html += HttpUtility.HtmlEncode(@"" + message.Body);
#endregion
MailMessage msg = new MailMessage();
msg.From = new MailAddress(ConfigurationManager.AppSettings["Email"].ToString());
msg.To.Add(new MailAddress(message.Destination));
msg.Subject = message.Subject;
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
SmtpClient smtpClient = new SmtpClient("smtp-mail.outlook.com", Convert.ToInt32(587));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["Email"].ToString(), ConfigurationManager.AppSettings["Password"].ToString());
smtpClient.Credentials = credentials;
smtpClient.EnableSsl = true;
smtpClient.Send(msg);
}
理想情况下,我希望用户不必将链接粘贴到网址栏中,而是能够点击超链接
【问题讨论】:
-
也许您没有将电子邮件正文设置为 HTML 而不是纯文本?我们看不到实际创建邮件消息的代码
-
我已将发送电子邮件操作添加到问题中。但我不确定在哪里将其设置为 HTML
标签: c# html asp.net-mvc