public ActionResult Contact(Contact model) { if (ModelState.IsValid) { try { // Get email server parameters from web.config... string smtpUsername = WebConfigurationManager.AppSettings["smtpUsername"]; string smtpPwd = WebConfigurationManager.AppSettings["smtpPwd"]; string smtpHost = WebConfigurationManager.AppSettings["smtpHost"]; string portString = WebConfigurationManager.AppSettings["smtpPort"]; //smtpPort = 25; string emailTo = WebConfigurationManager.AppSettings["emailTo"]; int smtpPort; bool parsed = Int32.TryParse(portString, out smtpPort); if (!parsed) { ModelState.AddModelError("", "Send Mail failed. SMTP Port in Web.config is not a valid port number."); return View(model); } string emailSubject = String.Format("Message from {0} {1} via Ironwood Consulting contact form", model.FirstName, model.LastName); string emailFrom = model.Email; string emailBody = String.Format("<h2>Contact form submission</h2><p><strong>From:</strong> {0} {1}</p><p><strong>From Email:</strong> {2}</p><p><strong>Message:</strong><br/>{3}</p>", model.FirstName, model.LastName, model.Email, model.Message); EmailService service = new EmailService(); bool sendMail = service.Send(smtpUsername, smtpPwd, smtpHost, smtpPort, emailTo, emailFrom, emailSubject, emailBody); if (sendMail) { return View("Success"); } else { ModelState.AddModelError("", "Send Mail failed."); return View(model); } } catch (Exception) { return View("Error"); } } return View(); }
public ActionResult Contact() { var contact = new Contact(); return View(contact); }