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

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