none
Code are working but emails not sending?? RRS feed

  • Question

  • 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, stringfrom) { 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(); stringfrom = "emailid"; sendemail2("Your Details", body, regInfo.emailid, from); Session["check"] = "Please check your email"; return View("check"); }

    publicvoid 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(); stringfrom = "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"); }

    i am using this code for verification of user .The code are running and giving no error ut emails are not sendng ??

    i have put my gmail apps security access:on but still noy working.i have try new and old gmail accounts also.

    • Moved by CoolDadTx Tuesday, November 13, 2018 6:38 PM ASP.NET related
    Tuesday, November 13, 2018 5:24 PM

All replies

  • You're going to have to debug this. The failure could be anywhere within the 3 different approaches you're trying. Given that you're silently eating exceptions it is most likely telling you exactly what is wrong but you don't see it because you're ignoring errors. Problem could be related to how you're getting data, the permissions on the server, etc. Please post this question in the ASP.NET forums.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, November 13, 2018 6:37 PM