MVC中使用电子邮件验证但它不起作用

MVC中使用电子邮件验证但它不起作用

本文介绍了我在ASP.NET MVC中使用电子邮件验证但它不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的asp.net mvc中使用电子邮件方法进行验证,并设置gmail允许应用程序安全访问级别低:但仍然无效。它没有抛出任何异常。早期这段代码完美无缺,电子邮件验证工作正确,但是从最近几天起它无法正常工作。



我有什么试过:



私人ActionResult SendEmail(MailMessage邮件)
{
SmtpClient client = new SmtpClient( );
client.Host =smtp.gmail.com;
client.Port = 587;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new System.Net.NetworkCredential(emailid,password);
try
{
client.Send(mail);

}
catch(Exception ex){return RedirectToAction(home);}
返回RedirectToAction(home2);
}



 public ActionResult sendemail2(string subjectText,string bodyText,string sendTo,string from)
{
string to,bcc,cc,subject,body;

to = sendTo.Trim();
bcc =;
cc =;
subject = subjectText;
StringBuilder sb = new StringBuilder();
sb.Append(bodyText);
body = sb.ToString();
MailMessage mail = new MailMessage();
mail.From = new MailAddress( from);
mail.To.Add(new MailAddress(to));
if(!string.IsNullOrEmpty(bcc))
{
mail.Bcc.Add(new MailAddress(bcc));
}
if(!string.IsNullOrEmpty(bcc))
{
mail.Bcc.Add(new MailAddress(cc));
}
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;

SendEmail(mail);
返回RedirectToAction(home);
}



 public ActionResult sendemail3(string textboxemailid)
{
string body = System .IO.File.ReadAllText(HostingEnvironment.MapPath(〜/ EmailTemplate /)+text+。cshtml);
var regInfo = db.xyztables.Where(x => x.emailid == textboxemailid).FirstOrDefault();

var pass = db.xyztables.Where(x => x.emailid == textboxemailid).Select(x => x.password).FirstOrDefault();

body = body.Replace(@ ViewBag.pass,pass);
body = body.ToString();
string from =emailid;
sendemail2(你的详细信息,正文,regInfo.emailid,来自);
会话[check] =请检查您的电子邮件;
返回查看(检查);
}



 public void sendemail4(int regid)
{
string body = System .IO.File.ReadAllText(HostingEnvironment.MapPath(〜/ EmailTemplate /)+Text+。cshtml);
var regInfo = db.xyztables.Where(x => x.id == regid).FirstOrDefault();
string regem = db.xyztables.Where(x => x.id == regid).Select(x => x.emailid).FirstOrDefault();
var url =http://xyz.com/+xyztables / Confirm?regem =+ regem;

body = body.Replace(@ ViewBag.ConfirmationLink,url);
body = body.ToString();
string from =emailid;
BuildEmailTemplate(激活你的账户,正文,regInfo.emailid,来自);

}

[HttpPost]

public ActionResult Signup(string textbox1emailid,string textboxname,string textboxpass,string Create)
{
var checkdb = db.xyztables.Where(x => x.emailid == textbox1emailid).FirstOrDefault();
if(checkdb == null)
{
xyztable table = new xyztable();
xyztable.emailid = textbox1emailid.ToString();
xyztable.name = textbox1name.ToString();
xyztable.password = textbox1pass.ToString();
xyztable.emailconfirmed = 0;
db.xyztables.Add(xyztable);
db.SaveChanges();
sendemail4(xyztable.id);
会话[check] =请检查您的emailid以确认您的帐户;
返回RedirectToAction(home);
}
else {Session [message] =Emailid已存在;
返回查看(登录);
}
}
解决方案

i am using email method for verification in my asp.net mvc and set gmail allow app security access level low :on but still its not working . Its is not throwing any exception .Earlier this code work perfectly and email verification is done corretly but from last few days it is not working .

What I have tried:

private ActionResult SendEmail(MailMessage mail)
       {
           SmtpClient client = new SmtpClient();
           client.Host = "smtp.gmail.com";
           client.Port = 587;
           client.EnableSsl = true;
           client.UseDefaultCredentials = false;
           client.DeliveryMethod = SmtpDeliveryMethod.Network;
           client.Credentials = new System.Net.NetworkCredential("emailid,"password");
           try
           {
               client.Send(mail);

           }
           catch (Exception ex) { return RedirectToAction("home"); }
           return RedirectToAction("home2");
       }


public ActionResult sendemail2(string subjectText, string bodyText, string sendTo, string from)
       {
           string to, bcc, cc, subject, body;

           to = sendTo.Trim();
           bcc = "";
           cc = "";
           subject = subjectText;
           StringBuilder sb = new StringBuilder();
           sb.Append(bodyText);
           body = sb.ToString();
           MailMessage mail = new MailMessage();
           mail.From = new MailAddress(from);
           mail.To.Add(new MailAddress(to));
           if (!string.IsNullOrEmpty(bcc))
           {
               mail.Bcc.Add(new MailAddress(bcc));
           }
           if (!string.IsNullOrEmpty(bcc))
           {
               mail.Bcc.Add(new MailAddress(cc));
           }
           mail.Subject = subject;
           mail.Body = body;
           mail.IsBodyHtml = true;

           SendEmail(mail);
           return RedirectToAction("home");
       }


public ActionResult sendemail3(string textboxemailid)
       {
           string body = System.IO.File.ReadAllText(HostingEnvironment.MapPath("~/EmailTemplate/") + "text" + ".cshtml");
           var regInfo = db.xyztables.Where(x => x.emailid == textboxemailid).FirstOrDefault();

           var pass = db.xyztables.Where(x => x.emailid == textboxemailid).Select(x => x.password).FirstOrDefault();

           body = body.Replace("@ViewBag.pass", pass);
           body = body.ToString();
           string from = "emailid";
           sendemail2("Your Details", body, regInfo.emailid, from);
           Session["check"] = "Please check your email";
           return View("check");
       }


public void sendemail4(int regid)
  {
      string body = System.IO.File.ReadAllText(HostingEnvironment.MapPath("~/EmailTemplate/") + "Text" + ".cshtml");
      var regInfo = db.xyztables.Where(x => x.id == regid).FirstOrDefault();
      string regem = db.xyztables.Where(x => x.id == regid).Select(x => x.emailid).FirstOrDefault();
      var url = "http://xyz.com/" + "xyztables/Confirm?regem="+ regem;

      body = body.Replace("@ViewBag.ConfirmationLink", url);
      body = body.ToString();
      string from = "emailid";
      BuildEmailTemplate("Activate your Account", body, regInfo.emailid,from);

  }

  [HttpPost]

  public ActionResult Signup(string textbox1emailid, string textboxname, string textboxpass ,  string Create)
  {
      var checkdb = db.xyztables.Where(x => x.emailid == textbox1emailid).FirstOrDefault();
      if (checkdb == null)
      {
          xyztable table = new xyztable();
          xyztable.emailid = textbox1emailid.ToString();
          xyztable.name = textbox1name.ToString();
          xyztable.password = textbox1pass.ToString();
          xyztable.emailconfirmed = 0;
          db.xyztables.Add(xyztable);
          db.SaveChanges();
          sendemail4(xyztable.id);
          Session["check"] = "Please check your emailid to confirm your Account";
          return RedirectToAction("home");
      }
      else { Session["message"] = "Emailid already exists";
          return View("Signin");
      }
         }
解决方案


这篇关于我在ASP.NET MVC中使用电子邮件验证但它不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:41