The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ContactController.cs 3.2KB

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