【问题标题】:Email link in controller控制器中的电子邮件链接
【发布时间】:2020-10-23 20:49:14
【问题描述】:

我在注册系统后执行确认,代码运行正常,有一个小问题。问题是用户必须点击链接,但这就是它在下图中的显示方式。

图片电子邮件:

我实际上不知道我在哪里错过了它,因为我希望链接正确显示并且不显示生成的令牌。如果您可以检查第一个“链接”,则它是不可点击的,其次,另一个链接会显示所有内容。

身份配置

  void sendMail(IdentityMessage message)
    {
        #region formatter
        string text = string.Format("Please click on this link to {0}: {1}", message.Subject, message.Body);
        string html = "Please confirm your account by clicking this link: <a href=\'" + message.Body + "\'>link</a><br/>";

        html += HttpUtility.HtmlEncode(@"Or click on the copy the following link on the browser:" + message.Body);
        #endregion

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(ConfigurationManager.AppSettings["Email"].ToString());
        msg.To.Add(new MailAddress(message.Destination));
        msg.IsBodyHtml = true;
        msg.Body = message.Body;
        msg.Subject = message.Subject;
        msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
        msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

        SmtpClient smtpClient = new SmtpClient("smtp.gmail.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);

    }

账户控制人

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Name = model.Name, Address = model.Address, PhoneNumber = model.PhoneNumber };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                //Assign Role to user Here
                await this.UserManager.AddToRoleAsync(user.Id, model.RoleName);
                //Ends Here 

                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);


                // Send an email with this link
                string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");


                if (UserManager.IsInRole(user.Id, "User"))
                {
                    return RedirectToAction("Index", "Shop");
                }

                if (UserManager.IsInRole(user.Id, "Admin"))
                {
                    return RedirectToAction("Index", "Admin");
                }

                //return RedirectToAction("Index", "Shop");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

【问题讨论】:

  • 您确定需要对 html 进行编码吗?
  • 对 HTML 进行编码会将 &amp;lt; 转换为 &amp;lt;,然后在电子邮件应用程序中将其呈现为 &amp;lt;,并以文本而不是 HTML 形式显示
  • 我需要隐藏这个长链接并做出正确的@johnny5

标签: c# asp.net asp.net-mvc smtpclient


【解决方案1】:

我设法做到了。

var callbackUrl= Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code });
var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, callbackUrl);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", " " + link + "");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2014-02-18
    • 2012-09-10
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    相关资源
    最近更新 更多