diff --git a/Project-Unite/App_Start/IdentityConfig.cs b/Project-Unite/App_Start/IdentityConfig.cs index db5889a..b9ca64b 100644 --- a/Project-Unite/App_Start/IdentityConfig.cs +++ b/Project-Unite/App_Start/IdentityConfig.cs @@ -18,45 +18,30 @@ namespace Project_Unite { public class EmailService : IIdentityMessageService { - public async Task SendAsync(IdentityMessage message) + public async Task SendAsync(IdentityMessage msg) { try { var siteConfig = new ApplicationDbContext().Configs.FirstOrDefault(); + var message = new MailMessage(); + message.To.Add(new MailAddress(msg.Destination)); + message.Subject = "[ShiftOS] " + msg.Subject; + message.Body = msg.Body; + message.IsBodyHtml = true; - var smtp = new SmtpClient(siteConfig.SMTPServer, siteConfig.SMTPPort); - smtp.UseDefaultCredentials = false; - if (siteConfig.UseTLSEncryption) - smtp.EnableSsl = true; //This is misleading... We want TLS but all we have is SSL. Oh well. - smtp.UseDefaultCredentials = false; - smtp.Credentials = new NetworkCredential(siteConfig.SMTPUsername, siteConfig.SMTPPassword); - var sMsg = new MailMessage(siteConfig.SMTPReturnAddress, message.Destination); - - sMsg.Body = @" - -

Message from the ShiftOS staff

- -

" + CommonMark.CommonMarkConverter.Convert(message.Body) + "

"; - sMsg.Subject = $"[{siteConfig.SiteName}] " + message.Subject; - sMsg.IsBodyHtml = true; - smtp.DeliveryMethod = SmtpDeliveryMethod.Network; - var db = new ApplicationDbContext(); - db.AuditLogs.Add(new AuditLog("system", AuditLogLevel.Admin, $"Email sending...

To: {sMsg.To}
Subject:
{sMsg.Subject}
Invoker IP:{HttpContext.Current.Request.UserHostAddress}")); - db.SaveChanges(); - smtp.SendCompleted += async (o, a) => + using (var smtp = new SmtpClient()) { - var alog = new AuditLog("system", AuditLogLevel.Admin, ""); - if (a.Cancelled == true) - alog.Description += "Send cancelled.
"; - if (a.Error == null) - alog.Description += "No errors detected.
"; - else - alog.Description += $"Error:
{a.Error}
"; - var ndb = new ApplicationDbContext(); - ndb.AuditLogs.Add(alog); - await ndb.SaveChangesAsync(); - }; - smtp.Send(sMsg); + var credential = new NetworkCredential + { + UserName = siteConfig.SMTPUsername, + Password = siteConfig.SMTPPassword + }; + smtp.Credentials = credential; + smtp.Host = siteConfig.SMTPServer; + smtp.Port = siteConfig.SMTPPort; + smtp.EnableSsl = siteConfig.UseTLSEncryption; + await smtp.SendMailAsync(message); + } } catch (Exception ex) {