The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
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.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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(Config.ContactConfig.EmailAccount.EmailAddress, Config.SupportEmail);
  60. mail.Subject = string.Format("Support Message from: {0} <{1}>", model.Name, model.Email);
  61. mail.Body = string.Format(@"
  62. New Support Message from: {0} <{1}>
  63. ---------------------------------
  64. Subject: {2}
  65. Message: {3}", model.Name, model.Email, model.Subject, model.Message);
  66. mail.BodyEncoding = UTF8Encoding.UTF8;
  67. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.Never;
  68. client.Send(mail);
  69. }
  70. catch (Exception ex)
  71. {
  72. return Json(new { error = "Error submitting message. Exception: " + ex.Message });
  73. }
  74. return Json(new { result = "true" });
  75. }
  76. return Json(new { error = "Contact Form is disabled" });
  77. }
  78. else
  79. {
  80. return View(model);
  81. }
  82. }
  83. }
  84. }