From 016df19d287cf07dbb666f1c87e35fe4440a432f Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Wed, 16 Dec 2015 12:46:54 -0800 Subject: [PATCH] Contact - Added SMTP config and sending of mail when Contact message sent --- .../Contact/Controllers/ContactController.cs | 32 +++++++++++++++++ .../Areas/Contact/Views/Contact/Index.cshtml | 6 +++- Teknik/Configuration/Config.cs | 35 ++++++++++++------- Teknik/Configuration/SMTPConfig.cs | 32 +++++++++++++++++ Teknik/Teknik.csproj | 1 + 5 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 Teknik/Configuration/SMTPConfig.cs diff --git a/Teknik/Areas/Contact/Controllers/ContactController.cs b/Teknik/Areas/Contact/Controllers/ContactController.cs index 2784cd2..52e1ba1 100644 --- a/Teknik/Areas/Contact/Controllers/ContactController.cs +++ b/Teknik/Areas/Contact/Controllers/ContactController.cs @@ -3,10 +3,12 @@ using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; +using System.Net.Mail; using Teknik.Controllers; using Teknik.Areas.Contact.ViewModels; using Teknik.Areas.Contact.Models; using Teknik.Models; +using System.Text; namespace Teknik.Areas.Contact.Controllers { @@ -32,6 +34,36 @@ namespace Teknik.Areas.Contact.Controllers { if (model.Insert()) { + try + { + // Let's also email the message to support + SmtpClient client = new SmtpClient(); + client.Host = Config.SMTPConfig.Host; + client.Port = Config.SMTPConfig.Port; + client.EnableSsl = Config.SMTPConfig.SSL; + client.DeliveryMethod = SmtpDeliveryMethod.Network; + client.UseDefaultCredentials = true; + client.Credentials = new System.Net.NetworkCredential(Config.SMTPConfig.Username, Config.SMTPConfig.Password); + client.Timeout = 5000; + + MailMessage mail = new MailMessage(Config.SupportEmail, Config.SupportEmail); + mail.Subject = string.Format("Support Message from: {0} <{1}>", model.Name, model.Email); + mail.Body = string.Format(@" +New Support Message from: {0} <{1}> + +--------------------------------- +Subject: {2} +Message: {3}", model.Name, model.Email, model.Subject, model.Message); + mail.BodyEncoding = UTF8Encoding.UTF8; + mail.DeliveryNotificationOptions = DeliveryNotificationOptions.Never; + + client.Send(mail); + } + catch (Exception ex) + { + return Json(new { error = "Error submitting message. Exception: " + ex.Message}); + } + return Json(new { result = "true" }); } return Json(new { error = "Error submitting message." }); diff --git a/Teknik/Areas/Contact/Views/Contact/Index.cshtml b/Teknik/Areas/Contact/Views/Contact/Index.cshtml index 5bd7997..2f4f9d2 100644 --- a/Teknik/Areas/Contact/Views/Contact/Index.cshtml +++ b/Teknik/Areas/Contact/Views/Contact/Index.cshtml @@ -68,7 +68,11 @@
Where to find us
- /g/ Technology on IRC
+ Teknik on Rizon IRC
+ #Teknik (irc.rizon.net)
+
+
+ /g/ Technology on Rizon IRC
#/g/technology (irc.rizon.net)
diff --git a/Teknik/Configuration/Config.cs b/Teknik/Configuration/Config.cs index e4e4bfe..456cd31 100644 --- a/Teknik/Configuration/Config.cs +++ b/Teknik/Configuration/Config.cs @@ -12,19 +12,28 @@ namespace Teknik private ReaderWriterLockSlim _ConfigFileRWLock; private JsonSerializerSettings _JsonSettings; - private bool _DevEnvironment; - private string _Title; - private string _Description; - private string _Author; - private string _Host; - private string _BitcoinAddress; + private bool _DevEnvironment; + private string _Title; + private string _Description; + private string _Author; + private string _Host; + private SMTPConfig _SMTPConfig; + private string _SupportEmail; + private string _BitcoinAddress; - public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } } - public string Title { get { return _Title; } set { _Title = value; } } - public string Description { get { return _Description; } set { _Description = value; } } - public string Author { get { return _Author; } set { _Author = value; } } - public string Host { get { return _Host; } set { _Host = value; } } - public string BitcoinAddress { get { return _BitcoinAddress; } set { _BitcoinAddress = value; } } + public bool DevEnvironment { get { return _DevEnvironment; } set { _DevEnvironment = value; } } + + public string Title { get { return _Title; } set { _Title = value; } } + public string Description { get { return _Description; } set { _Description = value; } } + public string Author { get { return _Author; } set { _Author = value; } } + + public string Host { get { return _Host; } set { _Host = value; } } + + public SMTPConfig SMTPConfig { get { return _SMTPConfig; } set { _SMTPConfig = value; } } + + public string SupportEmail { get { return _SupportEmail; } set { _SupportEmail = value; } } + + public string BitcoinAddress { get { return _BitcoinAddress; } set { _BitcoinAddress = value; } } public Config() { @@ -43,6 +52,8 @@ namespace Teknik Description = String.Empty; Author = String.Empty; Host = String.Empty; + SMTPConfig = new SMTPConfig(); + SupportEmail = string.Empty; BitcoinAddress = string.Empty; } diff --git a/Teknik/Configuration/SMTPConfig.cs b/Teknik/Configuration/SMTPConfig.cs new file mode 100644 index 0000000..cb49b72 --- /dev/null +++ b/Teknik/Configuration/SMTPConfig.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net.Mail; + +namespace Teknik +{ + public class SMTPConfig + { + public string Host { get; set; } + public int Port { get; set; } + public string Username { get; set; } + public string Password { get; set; } + public bool SSL { get; set; } + + public SMTPConfig() + { + SetDefaults(); + } + + public void SetDefaults() + { + Host = string.Empty; + Port = 25; + Username = string.Empty; + Password = string.Empty; + SSL = false; + } + } +} diff --git a/Teknik/Teknik.csproj b/Teknik/Teknik.csproj index b43b20a..25ffcc3 100644 --- a/Teknik/Teknik.csproj +++ b/Teknik/Teknik.csproj @@ -155,6 +155,7 @@ +