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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Net.Mail;
  6. using Teknik.Controllers;
  7. using Teknik.Areas.Contact.ViewModels;
  8. using Teknik.Areas.Contact.Models;
  9. using Teknik.Models;
  10. using System.Text;
  11. using Teknik.Filters;
  12. using Teknik.Attributes;
  13. using Microsoft.Extensions.Logging;
  14. using Teknik.Configuration;
  15. using Teknik.Data;
  16. using Microsoft.AspNetCore.Authorization;
  17. using Microsoft.AspNetCore.Mvc;
  18. using Teknik.Logging;
  19. namespace Teknik.Areas.Contact.Controllers
  20. {
  21. [TeknikAuthorize]
  22. [Area("Contact")]
  23. public class ContactController : DefaultController
  24. {
  25. public ContactController(ILogger<Logger> logger, Config config, TeknikEntities dbContext) : base(logger, config, dbContext) { }
  26. [AllowAnonymous]
  27. public IActionResult Index()
  28. {
  29. ViewBag.Title = "Contact - " + _config.Title;
  30. ViewBag.Description = "Contact Teknik Support";
  31. return View(new ContactViewModel());
  32. }
  33. [HttpPost]
  34. [AllowAnonymous]
  35. public IActionResult Submit(ContactViewModel model)
  36. {
  37. if (ModelState.IsValid)
  38. {
  39. if (_config.ContactConfig.Enabled)
  40. {
  41. try
  42. {
  43. // Insert the message into the DB
  44. Models.Contact newContact = new Models.Contact();
  45. newContact.Name = model.Name;
  46. newContact.Email = model.Email;
  47. newContact.Subject = model.Subject;
  48. newContact.Message = model.Message;
  49. newContact.DateAdded = DateTime.Now;
  50. _dbContext.Contact.Add(newContact);
  51. _dbContext.SaveChanges();
  52. // Let's also email the message to support
  53. SmtpClient client = new SmtpClient();
  54. client.Host = _config.ContactConfig.EmailAccount.Host;
  55. client.Port = _config.ContactConfig.EmailAccount.Port;
  56. client.EnableSsl = _config.ContactConfig.EmailAccount.SSL;
  57. client.DeliveryMethod = SmtpDeliveryMethod.Network;
  58. client.UseDefaultCredentials = true;
  59. client.Credentials = new System.Net.NetworkCredential(_config.ContactConfig.EmailAccount.Username, _config.ContactConfig.EmailAccount.Password);
  60. client.Timeout = 5000;
  61. MailMessage mail = new MailMessage(new MailAddress(model.Email, model.Name), new MailAddress(_config.SupportEmail, "Teknik Support"));
  62. mail.Sender = new MailAddress(_config.ContactConfig.EmailAccount.EmailAddress);
  63. mail.Subject = model.Subject;
  64. mail.Body = model.Message;
  65. mail.BodyEncoding = UTF8Encoding.UTF8;
  66. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.Never;
  67. client.Send(mail);
  68. }
  69. catch (Exception ex)
  70. {
  71. return Json(new { error = "Error submitting message. Exception: " + ex.Message });
  72. }
  73. return Json(new { result = "true" });
  74. }
  75. return Json(new { error = "Contact Form is disabled" });
  76. }
  77. else
  78. {
  79. return View(model);
  80. }
  81. }
  82. }
  83. }