Skip to content

Commit

Permalink
Refactor emailing behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
tymorrow committed Jun 23, 2024
1 parent 040c296 commit 6755cf0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
18 changes: 16 additions & 2 deletions src/Dsp.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using Data.Entities;
using Dsp.Services;
using Elmah;
using Extensions;
using MarkdownSharp;
using Microsoft.AspNet.Identity;
Expand Down Expand Up @@ -155,8 +156,21 @@ public async Task<ActionResult> TestEmail()
{
var userId = User.Identity.GetUserId<int>();
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);
}

Expand Down
38 changes: 8 additions & 30 deletions src/Dsp.Web/Extensions/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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,
})
{
Expand Down Expand Up @@ -117,27 +117,5 @@ public static async Task<string> TryToSendSoberSchedule(ISoberService soberServi

return "OK";
}

public static async Task<string> 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";
}
}
}
2 changes: 1 addition & 1 deletion src/Dsp.Web/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</sectionGroup>
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=dspdb;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="DefaultConnection" connectionString="Data Source=(local);Initial Catalog=dspdb;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
Expand Down

0 comments on commit 6755cf0

Please sign in to comment.