public ActionResult Index(Contact c) { if (ModelState.IsValid) { MailMessage message = new MailMessage(); SmtpClient smtpClient = new SmtpClient(); try { MailAddress fromAddress = new MailAddress(c.Email); message.From = fromAddress; message.To.Add("*****@*****.**"); message.Subject = "commet from Neecap website"; message.IsBodyHtml = true; message.Body = "From: " + c.Email + " Message: " + c.Comment; smtpClient.Host = "smtp.gmail.com"; smtpClient.Port = 587; smtpClient.EnableSsl = true; smtpClient.UseDefaultCredentials = true; smtpClient.Credentials = new System.Net.NetworkCredential("*****@*****.**", "Password123"); smtpClient.Send(message); return View("Success"); } catch (Exception) { return View("Error"); } } return View(); }
// GET: Contact //[HttpPost] public ActionResult Index(Contact c) { string Message = "There are a few errors"; if (ModelState.IsValid) { return View("Success"); } if (Request.IsAjaxRequest()) { return new JsonResult { ContentEncoding = Encoding.UTF8, Data = new { success = true, Message = Message } }; } TempData["Message"] = Message; return View(); }