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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 = "Contact - " + Config.Title;
  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. try
  32. {
  33. // Insert the message into the DB
  34. Models.Contact newContact = db.Contact.Create();
  35. newContact.Name = model.Name;
  36. newContact.Email = model.Email;
  37. newContact.Subject = model.Subject;
  38. newContact.Message = model.Message;
  39. newContact.DateAdded = DateTime.Now;
  40. db.Contact.Add(newContact);
  41. db.SaveChanges();
  42. // Let's also email the message to support
  43. SmtpClient client = new SmtpClient();
  44. client.Host = Config.SMTPConfig.Host;
  45. client.Port = Config.SMTPConfig.Port;
  46. client.EnableSsl = Config.SMTPConfig.SSL;
  47. client.DeliveryMethod = SmtpDeliveryMethod.Network;
  48. client.UseDefaultCredentials = true;
  49. client.Credentials = new System.Net.NetworkCredential(Config.SMTPConfig.Username, Config.SMTPConfig.Password);
  50. client.Timeout = 5000;
  51. MailMessage mail = new MailMessage(Config.SupportEmail, Config.SupportEmail);
  52. mail.Subject = string.Format("Support Message from: {0} <{1}>", model.Name, model.Email);
  53. mail.Body = string.Format(@"
  54. New Support Message from: {0} <{1}>
  55. ---------------------------------
  56. Subject: {2}
  57. Message: {3}", model.Name, model.Email, model.Subject, model.Message);
  58. mail.BodyEncoding = UTF8Encoding.UTF8;
  59. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.Never;
  60. client.Send(mail);
  61. }
  62. catch (Exception ex)
  63. {
  64. return Json(new { error = "Error submitting message. Exception: " + ex.Message});
  65. }
  66. return Json(new { result = "true" });
  67. }
  68. else
  69. {
  70. return View(model);
  71. }
  72. }
  73. }
  74. }