The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContactController.cs 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Net.Mail;
  7. using Teknik.Controllers;
  8. using Teknik.Areas.Contact.ViewModels;
  9. using Teknik.Areas.Contact.Models;
  10. using Teknik.Models;
  11. using System.Text;
  12. namespace Teknik.Areas.Contact.Controllers
  13. {
  14. public class ContactController : DefaultController
  15. {
  16. private TeknikEntities db = new TeknikEntities();
  17. // GET: Contact/Contact
  18. [AllowAnonymous]
  19. public ActionResult Index()
  20. {
  21. ViewBag.Title = Config.Title + " - Contact";
  22. ViewBag.Message = "Contact Teknik Support";
  23. return View(new ContactViewModel());
  24. }
  25. [HttpPost]
  26. [AllowAnonymous]
  27. public ActionResult Submit(ContactViewModel model)
  28. {
  29. if (ModelState.IsValid)
  30. {
  31. if (model.Insert())
  32. {
  33. try
  34. {
  35. // Let's also email the message to support
  36. SmtpClient client = new SmtpClient();
  37. client.Host = Config.SMTPConfig.Host;
  38. client.Port = Config.SMTPConfig.Port;
  39. client.EnableSsl = Config.SMTPConfig.SSL;
  40. client.DeliveryMethod = SmtpDeliveryMethod.Network;
  41. client.UseDefaultCredentials = true;
  42. client.Credentials = new System.Net.NetworkCredential(Config.SMTPConfig.Username, Config.SMTPConfig.Password);
  43. client.Timeout = 5000;
  44. MailMessage mail = new MailMessage(Config.SupportEmail, Config.SupportEmail);
  45. mail.Subject = string.Format("Support Message from: {0} <{1}>", model.Name, model.Email);
  46. mail.Body = string.Format(@"
  47. New Support Message from: {0} <{1}>
  48. ---------------------------------
  49. Subject: {2}
  50. Message: {3}", model.Name, model.Email, model.Subject, model.Message);
  51. mail.BodyEncoding = UTF8Encoding.UTF8;
  52. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.Never;
  53. client.Send(mail);
  54. }
  55. catch (Exception ex)
  56. {
  57. return Json(new { error = "Error submitting message. Exception: " + ex.Message});
  58. }
  59. return Json(new { result = "true" });
  60. }
  61. return Json(new { error = "Error submitting message." });
  62. }
  63. else
  64. {
  65. return View(model);
  66. }
  67. }
  68. }
  69. }