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 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. using Teknik.Filters;
  13. using Teknik.Attributes;
  14. namespace Teknik.Areas.Contact.Controllers
  15. {
  16. [TeknikAuthorize]
  17. public class ContactController : DefaultController
  18. {
  19. private TeknikEntities db = new TeknikEntities();
  20. // GET: Contact/Contact
  21. [TrackPageView]
  22. [AllowAnonymous]
  23. public ActionResult Index()
  24. {
  25. ViewBag.Title = "Contact - " + Config.Title;
  26. ViewBag.Description = "Contact Teknik Support";
  27. return View(new ContactViewModel());
  28. }
  29. [HttpPost]
  30. [AllowAnonymous]
  31. public ActionResult Submit(ContactViewModel model)
  32. {
  33. if (ModelState.IsValid)
  34. {
  35. if (Config.ContactConfig.Enabled)
  36. {
  37. try
  38. {
  39. // Insert the message into the DB
  40. Models.Contact newContact = db.Contact.Create();
  41. newContact.Name = model.Name;
  42. newContact.Email = model.Email;
  43. newContact.Subject = model.Subject;
  44. newContact.Message = model.Message;
  45. newContact.DateAdded = DateTime.Now;
  46. db.Contact.Add(newContact);
  47. db.SaveChanges();
  48. // Let's also email the message to support
  49. SmtpClient client = new SmtpClient();
  50. client.Host = Config.ContactConfig.EmailAccount.Host;
  51. client.Port = Config.ContactConfig.EmailAccount.Port;
  52. client.EnableSsl = Config.ContactConfig.EmailAccount.SSL;
  53. client.DeliveryMethod = SmtpDeliveryMethod.Network;
  54. client.UseDefaultCredentials = true;
  55. client.Credentials = new System.Net.NetworkCredential(Config.ContactConfig.EmailAccount.Username, Config.ContactConfig.EmailAccount.Password);
  56. client.Timeout = 5000;
  57. MailMessage mail = new MailMessage(Config.ContactConfig.EmailAccount.EmailAddress, Config.SupportEmail);
  58. mail.Subject = string.Format("Support Message from: {0} <{1}>", model.Name, model.Email);
  59. mail.Body = string.Format(@"
  60. New Support Message from: {0} <{1}>
  61. ---------------------------------
  62. Subject: {2}
  63. Message: {3}", model.Name, model.Email, model.Subject, model.Message);
  64. mail.BodyEncoding = UTF8Encoding.UTF8;
  65. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.Never;
  66. client.Send(mail);
  67. }
  68. catch (Exception ex)
  69. {
  70. return Json(new { error = "Error submitting message. Exception: " + ex.Message });
  71. }
  72. return Json(new { result = "true" });
  73. }
  74. return Json(new { error = "Contact Form is disabled" });
  75. }
  76. else
  77. {
  78. return View(model);
  79. }
  80. }
  81. }
  82. }