diff --git a/src/Dsp.Web/Controllers/HomeController.cs b/src/Dsp.Web/Controllers/HomeController.cs index f06d0da9..ee96072f 100644 --- a/src/Dsp.Web/Controllers/HomeController.cs +++ b/src/Dsp.Web/Controllers/HomeController.cs @@ -2,6 +2,7 @@ { using Data.Entities; using Dsp.Services; + using Elmah; using Extensions; using MarkdownSharp; using Microsoft.AspNet.Identity; @@ -155,8 +156,21 @@ public async Task TestEmail() { var userId = User.Identity.GetUserId(); var user = await UserManager.FindByIdAsync(userId); - var userEmail = user.Email; - var result = await EmailService.SendTestEmail(userEmail); + string TO = user.Email.ToString(); + const string SUBJECT = "Sphinx: Test Email"; + const string BODY = "This is a test email."; + string result = "OK"; + try + { + var emailService = new EmailService(); + await emailService.SendAsync(TO, SUBJECT, BODY); + } + catch (Exception e) + { + ErrorSignal.FromCurrentContext().Raise(e); + result = "Error - " + TO; + } + return Content(result); } diff --git a/src/Dsp.Web/Extensions/EmailService.cs b/src/Dsp.Web/Extensions/EmailService.cs index 1d29d898..6ef2e537 100644 --- a/src/Dsp.Web/Extensions/EmailService.cs +++ b/src/Dsp.Web/Extensions/EmailService.cs @@ -4,7 +4,6 @@ using Dsp.Data.Entities; using Dsp.Services; using Dsp.Services.Interfaces; - using Elmah; using Microsoft.AspNet.Identity; using RazorEngine; using RazorEngine.Templating; @@ -18,14 +17,15 @@ public class EmailService : IIdentityMessageService { public async Task SendAsync(IdentityMessage message) + { + await SendAsync(message.Destination, message.Subject, message.Body); + } + + public async Task SendAsync(string to, string subject, string body) { var smtpEndpoint = Environment.GetEnvironmentVariable("SPHINX_SMTP_ENDPOINT"); var smtpUsername = Environment.GetEnvironmentVariable("SPHINX_SMTP_USERNAME"); var smtpPassword = Environment.GetEnvironmentVariable("SPHINX_SMTP_PASSWORD"); - var fromAddress = new MailAddress(smtpUsername, "Sphinx"); - var toAddress = new MailAddress(message.Destination); - - SmtpClient client = new SmtpClient() { Host = smtpEndpoint, @@ -35,10 +35,10 @@ public async Task SendAsync(IdentityMessage message) UseDefaultCredentials = false, Credentials = new NetworkCredential(smtpUsername, smtpPassword) }; - using (var mailMessage = new MailMessage(fromAddress, toAddress) + using (var mailMessage = new MailMessage(smtpUsername, to) { - Subject = message.Subject, - Body = message.Body, + Subject = subject, + Body = body, IsBodyHtml = true, }) { @@ -117,27 +117,5 @@ public static async Task TryToSendSoberSchedule(ISoberService soberServi return "OK"; } - - public static async Task SendTestEmail(string destination) - { - var message = new IdentityMessage - { - Subject = "Sphinx: Test Email", - Body = "This is a test email.", - Destination = destination - }; - - try - { - var emailService = new EmailService(); - await emailService.SendAsync(message); - } - catch (Exception e) - { - ErrorSignal.FromCurrentContext().Raise(e); - return "Error"; - } - return "OK"; - } } } \ No newline at end of file diff --git a/src/Dsp.Web/Web.config b/src/Dsp.Web/Web.config index 7b4955a6..beb7d77d 100644 --- a/src/Dsp.Web/Web.config +++ b/src/Dsp.Web/Web.config @@ -10,7 +10,7 @@ - +