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.3KB

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