ASP.net MVC 5 项目 SMTP 功能错误 [重复]
Posted
技术标签:
【中文标题】ASP.net MVC 5 项目 SMTP 功能错误 [重复]【英文标题】:ASP.net MVC 5 project SMTP function error [duplicate] 【发布时间】:2020-01-06 19:32:54 【问题描述】:正如上面的标题,我创建了一个简单的登录和注册的项目。在程序内部,我使用 System.Net.Mail.SmtpException 将电子邮件验证发送到用户电子邮件。我已经创建了电子邮件,但是当我点击创建新帐户时,我也遇到了这个问题。
System.Net.Mail.SmtpException: 'The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
从我搜索的信息中,我发现有人说我需要对我的电子邮件进行两步验证。我做到了,但问题仍然存在。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using Food_Founder.Models;
namespace Food_Founder.Controllers
public class UserController : Controller
//Registration
[HttpGet]
public ActionResult Registration()
return View();
//Post Registration
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Registration([Bind(Exclude = "IsEmailVerified,ActivationCode")]User user)
//Model Validation
bool Status = false;
string message = "";
//Email is already exist
if (ModelState.IsValid)
#region Email is already exist
var isExist = IsEmailExist(user.Email);
if (isExist)
ModelState.AddModelError("EmailExist", "Email already exist");
return View(user);
#endregion
#region Generate Activation Code
user.ActivationCode = Guid.NewGuid();
#endregion
#region Password Hashing
user.Password = Crypto.Hash(user.Password);
user.ConfirmPassword = Crypto.Hash(user.ConfirmPassword);
#endregion
user.IsEmailVerified = false;
#region Save Data to Database
using (myDatabaseEntities myDatabase = new myDatabaseEntities())
myDatabase.Users.Add(user);
myDatabase.SaveChanges();
//Send Email to User
SendVerificationLinkEmail(user.Email, user.ActivationCode.ToString());
message = "Registration successfully done. Account activation link" +
"has been send to your Email:" + user.Email + "Please go check and activate your account";
Status = true;
#endregion
else
message = "Invalid Request";
ViewBag.Message = message;
ViewBag.Status = Status;
return View(user);
//Verify Email
//Verify Email Link
//Login
//Login POST
//Logout
[NonAction]
public Boolean IsEmailExist(string email)
using (myDatabaseEntities myDatabase = new myDatabaseEntities())
var v = myDatabase.Users.Where(a => a.Email == email).FirstOrDefault();
return v != null;
[NonAction]
public void SendVerificationLinkEmail(string email, string activationCode)
var verifyUrl = "/User/VerifyAccount/" + activationCode;
var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, verifyUrl);
var fromEmail = new MailAddress("yukwokyao2@gmail.com", "yukwokyao");
var toEmail = new MailAddress(email);
var fromEmailPassword = "********";
string subject = "Your account is successfully created!";
string body = "<br/><br/>We are excited to tell you that your FoodFounder account is" +
"successfully created. Please click the below link to verify your FoodFounder account" +
"<a href = '" + link + "' >" + link + "</a>";
var smtp = new SmtpClient
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromEmail.Address, fromEmailPassword)
;
using (var message = new MailMessage(fromEmail, toEmail)
Subject = subject,
Body = body,
IsBodyhtml = true
)
smtp.Send(message);
上面的代码是我创建的控制器类。我发现的问题就在那里,所以我没有在这里放任何其他设计html代码。最奇怪的是即使我遇到了这个问题,我注册的数据仍然可以键入到数据库中,但是这个烦人的问题仍然存在。任何地方,如果我错过了什么,请告诉我。谢谢。
【问题讨论】:
【参考方案1】:如果您将 Gmail 帐户用作 SMTP 服务器,则需要允许从 Google 所谓的“不太安全的应用程序”进行访问。您可以从 Google 设置中启用它。
【讨论】:
在这里试试link 我已经启用了,但错误仍然存在。以上是关于ASP.net MVC 5 项目 SMTP 功能错误 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
无私分享:从入门到精通ASP.NET MVC从0开始,一起搭框架做项目(5.3) 登录功能的实现,丰富数据表建立关联
ASP.NET Core分布式项目实战整理IdentityServer4 MVC授权Consent功能实现