public ActionResult Contacts(EmailModel model) { if (ModelState.IsValid) { try { MailSender.SendContactMail(model); model.Success = true; } catch (SmtpException) { model.Success = false; } } ViewBag.ModelEmail = model; if (Request.IsAjaxRequest()) { ViewBag.Layout = null; return PartialView("Contacts"); } ViewBag.Layout = "~/Views/_Layout.cshtml"; return View("Contacts", model); }
public static void SendContactMail(EmailModel model) { var message = new MailMessage { Subject = model.Subject, Body = String.Format("{0}.\n\nИмя - {1}\nТел - {2}\nEmail - {3}", model.Body, model.FromName, model.Telephone, model.From), IsBodyHtml = false }; message.To.Add(model.To); var mailSettings = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp"); var mailClient = new SmtpClient(mailSettings.Network.Host, mailSettings.Network.Port) { Credentials = new NetworkCredential(mailSettings.Network.UserName, mailSettings.Network.Password), EnableSsl = mailSettings.Network.EnableSsl }; message.From = new MailAddress(mailSettings.From); mailClient.Send(message); }