在ASP.NET项目中,确保以下步骤已正确完成:
string token = WebSecurity.GeneratePasswordResetToken(username);
string confirmationLink = Url.Action("ConfirmEmail", "Account", new { Token = token, Email = email }, protocol: Request.Url.Scheme);
string body = "Please confirm your email by clicking this link: " + confirmationLink;
WebMail.SmtpServer = "smtp.gmail.com";
WebMail.SmtpPort = 587;
WebMail.EnableSsl = true;
WebMail.UserName = "yourEmail@gmail.com";
WebMail.Password = "yourGmailPassword";
WebMail.From = "yourEmail@gmail.com";
WebMail.Send(to: email, subject: "Confirm your email", body: body);
在上面的代码中,我们首先通过传递用户名和电子邮件地址来生成令牌。然后,通过调用Url.Action方法来创建验证链接。最后,我们使用WebMail类中的Send方法来发送电子邮件。
[AllowAnonymous]
public ActionResult ConfirmEmail(string token, string email)
{
bool confirmed = WebSecurity.ConfirmAccount(email, token);
if (confirmed)
{
// perform any necessary action after confirmation
return View();