diff --git a/PlexRequests.Services/Notification/EmailMessageNotification.cs b/PlexRequests.Services/Notification/EmailMessageNotification.cs index cd4c6b0a2..311ef6d69 100644 --- a/PlexRequests.Services/Notification/EmailMessageNotification.cs +++ b/PlexRequests.Services/Notification/EmailMessageNotification.cs @@ -28,12 +28,13 @@ using System; using System.Net; using System.Net.Mail; using System.Threading.Tasks; - +using MimeKit; using NLog; using PlexRequests.Core; using PlexRequests.Core.SettingModels; using PlexRequests.Services.Interfaces; +using SmtpClient = MailKit.Net.Smtp.SmtpClient; namespace PlexRequests.Services.Notification { @@ -111,28 +112,28 @@ namespace PlexRequests.Services.Notification private async Task EmailNewRequest(NotificationModel model, EmailNotificationSettings settings) { - var message = new MailMessage + var message = new MimeMessage { - IsBodyHtml = true, - To = { new MailAddress(settings.RecipientEmail) }, - Body = $"Hello! The user '{model.User}' has requested {model.Title}! Please log in to approve this request. Request Date: {model.DateTime.ToString("f")}", - From = new MailAddress(settings.EmailSender), + Body = new TextPart("plain") { Text = $"Hello! The user '{model.User}' has requested {model.Title}! Please log in to approve this request. Request Date: {model.DateTime.ToString("f")}" }, Subject = $"Plex Requests: New request for {model.Title}!" }; + message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender)); + message.To.Add(new MailboxAddress(settings.RecipientEmail, settings.RecipientEmail)); + await Send(message, settings); } private async Task EmailIssue(NotificationModel model, EmailNotificationSettings settings) { - var message = new MailMessage + var message = new MimeMessage { - IsBodyHtml = true, - To = { new MailAddress(settings.RecipientEmail) }, - Body = $"Hello! The user '{model.User}' has reported a new issue {model.Body} for the title {model.Title}!", - From = new MailAddress(settings.EmailSender), + Body = new TextPart("plain") { Text = $"Hello! The user '{model.User}' has reported a new issue {model.Body} for the title {model.Title}!" }, Subject = $"Plex Requests: New issue for {model.Title}!" }; + message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender)); + message.To.Add(new MailboxAddress(settings.RecipientEmail, settings.RecipientEmail)); + await Send(message, settings); } @@ -144,33 +145,35 @@ namespace PlexRequests.Services.Notification await Task.FromResult(false); } - var message = new MailMessage + var message = new MimeMessage { - IsBodyHtml = true, - To = { new MailAddress(model.UserEmail) }, - Body = $"Hello! You requested {model.Title} on PlexRequests! This is now available on Plex! :)", - From = new MailAddress(settings.EmailSender), + Body = new TextPart("plain") { Text = $"Hello! You requested {model.Title} on PlexRequests! This is now available on Plex! :)" }, Subject = $"Plex Requests: {model.Title} is now available!" }; + message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender)); + message.To.Add(new MailboxAddress(model.UserEmail, model.UserEmail)); await Send(message, settings); } - private async Task Send(MailMessage message, EmailNotificationSettings settings) + private async Task Send(MimeMessage message, EmailNotificationSettings settings) { try { - using (var smtp = new SmtpClient(settings.EmailHost, settings.EmailPort)) + using (var client = new SmtpClient()) { - smtp.Credentials = new NetworkCredential(settings.EmailUsername, settings.EmailPassword); - smtp.EnableSsl = settings.Ssl; - await smtp.SendMailAsync(message).ConfigureAwait(false); + client.Connect(settings.EmailHost, settings.EmailPort, settings.Ssl); + + // Note: since we don't have an OAuth2 token, disable + // the XOAUTH2 authentication mechanism. + client.AuthenticationMechanisms.Remove("XOAUTH2"); + + client.Authenticate(settings.EmailUsername, settings.EmailPassword); + + await client.SendAsync(message); + await client.DisconnectAsync(true); } } - catch (SmtpException smtp) - { - Log.Error(smtp); - } catch (Exception e) { Log.Error(e); @@ -179,14 +182,13 @@ namespace PlexRequests.Services.Notification private async Task EmailTest(NotificationModel model, EmailNotificationSettings settings) { - var message = new MailMessage + var message = new MimeMessage { - IsBodyHtml = true, - To = { new MailAddress(settings.RecipientEmail) }, - Body = "This is just a test! Success!", - From = new MailAddress(settings.EmailSender), - Subject = "Plex Requests: Test Message!" + Body = new TextPart("plain") {Text= "This is just a test! Success!"}, + Subject = "Plex Requests: Test Message!", }; + message.From.Add(new MailboxAddress(settings.EmailSender, settings.EmailSender)); + message.To.Add(new MailboxAddress(settings.RecipientEmail, settings.RecipientEmail)); await Send(message, settings); } diff --git a/PlexRequests.UI/Modules/BaseAuthModule.cs b/PlexRequests.UI/Modules/BaseAuthModule.cs index 0bbad4a02..708a4cb49 100644 --- a/PlexRequests.UI/Modules/BaseAuthModule.cs +++ b/PlexRequests.UI/Modules/BaseAuthModule.cs @@ -56,12 +56,22 @@ namespace PlexRequests.UI.Modules } } - protected bool IsAdmin { get { - var claims = Context.CurrentUser.Claims.ToList(); - if(claims.Contains(UserClaims.Admin) || claims.Contains(UserClaims.PowerUser)){ - return true;} - return false; - } } + protected bool IsAdmin + { + get + { + if (Context.CurrentUser == null) + { + return false; + } + var claims = Context.CurrentUser.Claims.ToList(); + if (claims.Contains(UserClaims.Admin) || claims.Contains(UserClaims.PowerUser)) + { + return true; + } + return false; + } + } protected int DateTimeOffset { @@ -96,8 +106,8 @@ namespace PlexRequests.UI.Modules var redirectPath = string.IsNullOrEmpty(baseUrl) ? "~/userlogin" : $"~/{baseUrl}/userlogin"; - return Session[SessionKeys.UsernameKey] == null - ? Context.GetRedirect(redirectPath) + return Session[SessionKeys.UsernameKey] == null + ? Context.GetRedirect(redirectPath) : null; } } diff --git a/PlexRequests.UI/Views/Admin/Authentication.cshtml b/PlexRequests.UI/Views/Admin/Authentication.cshtml index 471b10903..badd56f05 100644 --- a/PlexRequests.UI/Views/Admin/Authentication.cshtml +++ b/PlexRequests.UI/Views/Admin/Authentication.cshtml @@ -164,7 +164,7 @@ return; } if (response.users.length > 1) { - $(response).each(function () { + $(response.users).each(function () { $('#users').append(""); }); } else {