【问题标题】:ASP.NET MVC Hyperlink from Email来自电子邮件的 ASP.NET MVC 超链接
【发布时间】: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


【解决方案1】:

听起来您的电子邮件是以纯文本而不是 HTML 格式发送的。

您可以通过设置 MailMessage 对象的 IsBodyHtml 属性来解决此问题。在你的情况下:

msg.IsBodyHtml = true;

这在此处记录:https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.isbodyhtml?view=netframework-4.8

【讨论】:

    猜你喜欢
    • 2015-09-27
    • 2011-06-11
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 2020-08-30
    • 2022-11-22
    • 2015-07-26
    相关资源
    最近更新 更多