Using Mailkit to fix #204

This commit is contained in:
tidusjar 2016-05-20 21:24:26 +01:00
parent 86fa887bce
commit 4fd3db1ae5
3 changed files with 52 additions and 40 deletions

View file

@ -28,12 +28,13 @@ using System;
using System.Net; using System.Net;
using System.Net.Mail; using System.Net.Mail;
using System.Threading.Tasks; using System.Threading.Tasks;
using MimeKit;
using NLog; using NLog;
using PlexRequests.Core; using PlexRequests.Core;
using PlexRequests.Core.SettingModels; using PlexRequests.Core.SettingModels;
using PlexRequests.Services.Interfaces; using PlexRequests.Services.Interfaces;
using SmtpClient = MailKit.Net.Smtp.SmtpClient;
namespace PlexRequests.Services.Notification namespace PlexRequests.Services.Notification
{ {
@ -111,28 +112,28 @@ namespace PlexRequests.Services.Notification
private async Task EmailNewRequest(NotificationModel model, EmailNotificationSettings settings) private async Task EmailNewRequest(NotificationModel model, EmailNotificationSettings settings)
{ {
var message = new MailMessage var message = new MimeMessage
{ {
IsBodyHtml = true, 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")}" },
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),
Subject = $"Plex Requests: New request for {model.Title}!" 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); await Send(message, settings);
} }
private async Task EmailIssue(NotificationModel model, EmailNotificationSettings settings) private async Task EmailIssue(NotificationModel model, EmailNotificationSettings settings)
{ {
var message = new MailMessage var message = new MimeMessage
{ {
IsBodyHtml = true, Body = new TextPart("plain") { Text = $"Hello! The user '{model.User}' has reported a new issue {model.Body} for the title {model.Title}!" },
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),
Subject = $"Plex Requests: New issue for {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); await Send(message, settings);
} }
@ -144,33 +145,35 @@ namespace PlexRequests.Services.Notification
await Task.FromResult(false); await Task.FromResult(false);
} }
var message = new MailMessage var message = new MimeMessage
{ {
IsBodyHtml = true, Body = new TextPart("plain") { Text = $"Hello! You requested {model.Title} on PlexRequests! This is now available on Plex! :)" },
To = { new MailAddress(model.UserEmail) },
Body = $"Hello! You requested {model.Title} on PlexRequests! This is now available on Plex! :)",
From = new MailAddress(settings.EmailSender),
Subject = $"Plex Requests: {model.Title} is now available!" 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); await Send(message, settings);
} }
private async Task Send(MailMessage message, EmailNotificationSettings settings) private async Task Send(MimeMessage message, EmailNotificationSettings settings)
{ {
try try
{ {
using (var smtp = new SmtpClient(settings.EmailHost, settings.EmailPort)) using (var client = new SmtpClient())
{ {
smtp.Credentials = new NetworkCredential(settings.EmailUsername, settings.EmailPassword); client.Connect(settings.EmailHost, settings.EmailPort, settings.Ssl);
smtp.EnableSsl = settings.Ssl;
await smtp.SendMailAsync(message).ConfigureAwait(false); // 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) catch (Exception e)
{ {
Log.Error(e); Log.Error(e);
@ -179,14 +182,13 @@ namespace PlexRequests.Services.Notification
private async Task EmailTest(NotificationModel model, EmailNotificationSettings settings) private async Task EmailTest(NotificationModel model, EmailNotificationSettings settings)
{ {
var message = new MailMessage var message = new MimeMessage
{ {
IsBodyHtml = true, Body = new TextPart("plain") {Text= "This is just a test! Success!"},
To = { new MailAddress(settings.RecipientEmail) }, Subject = "Plex Requests: Test Message!",
Body = "This is just a test! Success!",
From = new MailAddress(settings.EmailSender),
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); await Send(message, settings);
} }

View file

@ -56,12 +56,22 @@ namespace PlexRequests.UI.Modules
} }
} }
protected bool IsAdmin { get { protected bool IsAdmin
var claims = Context.CurrentUser.Claims.ToList(); {
if(claims.Contains(UserClaims.Admin) || claims.Contains(UserClaims.PowerUser)){ get
return true;} {
if (Context.CurrentUser == null)
{
return false; return false;
} } }
var claims = Context.CurrentUser.Claims.ToList();
if (claims.Contains(UserClaims.Admin) || claims.Contains(UserClaims.PowerUser))
{
return true;
}
return false;
}
}
protected int DateTimeOffset protected int DateTimeOffset
{ {

View file

@ -164,7 +164,7 @@
return; return;
} }
if (response.users.length > 1) { if (response.users.length > 1) {
$(response).each(function () { $(response.users).each(function () {
$('#users').append("<option>" + this + "</option>"); $('#users').append("<option>" + this + "</option>");
}); });
} else { } else {