C# ASP.net MVC 5项目SMTP函数错误
正如上面的标题,我创建了一个简单的登录和注册项目。在程序内部,我使用System.Net.Mail.SmtpException将电子邮件验证发送到用户电子邮件。我已经创建了电子邮件,但当我单击“创建新帐户”时,我也遇到了这个问题C# ASP.net MVC 5项目SMTP函数错误,c#,asp.net,email,C#,Asp.net,Email,正如上面的标题,我创建了一个简单的登录和注册项目。在程序内部,我使用System.Net.Mail.SmtpException将电子邮件验证发送到用户电子邮件。我已经创建了电子邮件,但当我单击“创建新帐户”时,我也遇到了这个问题 System.Net.Mail.SmtpException: 'The SMTP server requires a secure connection or the client was not authenticated. The server response w
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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Net.Mail;
使用System.Web;
使用System.Web.Mvc;
使用食物模型;
名称空间Food.u.Controllers
{
公共类UserController:Controller
{
//登记
[HttpGet]
公众行动结果登记()
{
返回视图();
}
//注册后
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果注册([Bind(Exclude=“IsEmailVerified,ActivationCode”)]用户)
{
//模型验证
布尔状态=假;
字符串消息=”;
//电子邮件已经存在
if(ModelState.IsValid)
{
#区域电子邮件已存在
var isExist=IsEmailExist(user.Email);
如果(存在)
{
AddModelError(“EmailExist”,“Email已经存在”);
返回视图(用户);
}
#端区
#区域生成激活码
user.ActivationCode=Guid.NewGuid();
#端区
#区域密码散列
user.Password=Crypto.Hash(user.Password);
user.ConfirmPassword=Crypto.Hash(user.ConfirmPassword);
#端区
user.IsEmailVerified=false;
#区域将数据保存到数据库
使用(myDatabaseEntities myDatabase=new myDatabaseEntities())
{
myDatabase.Users.Add(用户);
myDatabase.SaveChanges();
//向用户发送电子邮件
SendVerificationLinkEmail(user.Email,user.ActivationCode.ToString());
message=“注册成功。帐户激活链接”+
“已发送到您的电子邮件:”+用户。电子邮件+“请检查并激活您的帐户”;
状态=真;
}
#端区
}
其他的
{
message=“无效请求”;
}
ViewBag.Message=消息;
状态=状态;
返回视图(用户);
}
//验证电子邮件
//验证电子邮件链接
//登录
//登录帖子
//注销
[不行动]
公共邮件存在(字符串电子邮件)
{
使用(myDatabaseEntities myDatabase=new myDatabaseEntities())
{
var v=myDatabase.Users.Where(a=>a.Email==Email.FirstOrDefault();
返回v!=null;
}
}
[不行动]
public void sendVerificationLink电子邮件(字符串电子邮件、字符串激活代码)
{
var verifyUrl=“/User/VerifyAccount/”+激活代码;
var link=Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery,verifyUrl);
var fromEmail=新邮件地址(“yukwokyao2@gmail.com","育沃基奥";;
var toEmail=新邮件地址(电子邮件);
var fromEmailPassword=“*******”;
string subject=“您的帐户已成功创建!”;
string body=“
我们很高兴告诉您,您的FoodFounder帐户是”+
“已成功创建。请单击下面的链接以验证您的FoodFounder帐户”+
"";
var smtp=新SmtpClient
{
Host=“smtp.gmail.com”,
端口=587,
EnableSsl=true,
DeliveryMethod=SmtpDeliveryMethod.Network,
UseDefaultCredentials=false,
凭据=新网络凭据(fromEmail.Address,fromEmailPassword)
};
使用(var消息=新邮件消息(从电子邮件到电子邮件)
{
主语,
身体,
IsBodyHtml=true
})
smtp.Send(message);
}
}
}
上面的代码是我创建的控制器类。我发现的问题就在这里,所以我没有在这里放任何其他设计html代码。最奇怪的是,即使我遇到了这个问题,我注册的数据仍然可以输入数据库,但这个恼人的问题仍然存在。任何地方,如果有什么我想放的,请通知我你。。谢谢。如果您使用您的Gmail帐户作为SMTP服务器,您需要允许从谷歌称之为“不太安全的应用”进行访问。你可以从谷歌设置中启用它。试试这里,我已经启用了,但错误仍然存在。