Fixes #854 Fixes #884 Fixes #951 Fixes #954 (cherry picked from commit a8b6f70be1860aa502795f0dd30299c87d54dbbe)pull/1092/head
parent
214fa4c06e
commit
811c84a845
@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using FizzWare.NBuilder;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.Notifications.Email;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using NzbDrone.Test.Common;
|
||||
|
||||
namespace NzbDrone.Core.Test.NotificationTests.EmailTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class EmailSettingsValidatorFixture : CoreTest<EmailSettingsValidator>
|
||||
{
|
||||
private EmailSettings _emailSettings;
|
||||
private TestValidator<EmailSettings> _validator;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_validator = new TestValidator<EmailSettings>
|
||||
{
|
||||
v => v.RuleFor(s => s).SetValidator(Subject)
|
||||
};
|
||||
|
||||
_emailSettings = Builder<EmailSettings>.CreateNew()
|
||||
.With(s => s.Server = "someserver")
|
||||
.With(s => s.Port = 567)
|
||||
.With(s => s.From = "readarr@readarr.com")
|
||||
.With(s => s.To = new string[] { "readarr@readarr.com" })
|
||||
.Build();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_be_valid_if_all_settings_valid()
|
||||
{
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_be_valid_if_port_is_out_of_range()
|
||||
{
|
||||
_emailSettings.Port = 900000;
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_be_valid_if_server_is_empty()
|
||||
{
|
||||
_emailSettings.Server = "";
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_be_valid_if_from_is_empty()
|
||||
{
|
||||
_emailSettings.From = "";
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[TestCase("readarr")]
|
||||
[TestCase("readarr@readarr")]
|
||||
[TestCase("readarr.com")]
|
||||
public void should_not_be_valid_if_to_is_invalid(string email)
|
||||
{
|
||||
_emailSettings.To = new string[] { email };
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[TestCase("readarr")]
|
||||
[TestCase("readarr@readarr")]
|
||||
[TestCase("readarr.com")]
|
||||
public void should_not_be_valid_if_cc_is_invalid(string email)
|
||||
{
|
||||
_emailSettings.CC = new string[] { email };
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[TestCase("readarr")]
|
||||
[TestCase("readarr@readarr")]
|
||||
[TestCase("readarr.com")]
|
||||
public void should_not_be_valid_if_bcc_is_invalid(string email)
|
||||
{
|
||||
_emailSettings.Bcc = new string[] { email };
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_be_valid_if_to_bcc_cc_are_all_empty()
|
||||
{
|
||||
_emailSettings.To = Array.Empty<string>();
|
||||
_emailSettings.CC = Array.Empty<string>();
|
||||
_emailSettings.Bcc = Array.Empty<string>();
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Mail;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Http;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Email
|
||||
{
|
||||
public interface IEmailService
|
||||
{
|
||||
void SendEmail(EmailSettings settings, string subject, string body, bool htmlBody = false, List<string> attachmentUrls = null);
|
||||
ValidationFailure Test(EmailSettings settings);
|
||||
}
|
||||
|
||||
public class EmailService : IEmailService
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public EmailService(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void SendEmail(EmailSettings settings, string subject, string body, bool htmlBody = false, List<string> attachmentUrls = null)
|
||||
{
|
||||
var email = new MailMessage();
|
||||
email.From = new MailAddress(settings.From);
|
||||
|
||||
settings.To.ToList().ForEach(x => email.To.Add(x));
|
||||
settings.CC.ToList().ForEach(x => email.CC.Add(x));
|
||||
settings.Bcc.ToList().ForEach(x => email.Bcc.Add(x));
|
||||
|
||||
email.Subject = subject;
|
||||
email.Body = body;
|
||||
email.IsBodyHtml = htmlBody;
|
||||
|
||||
if (attachmentUrls != null)
|
||||
{
|
||||
foreach (var url in attachmentUrls)
|
||||
{
|
||||
email.Attachments.Add(new Attachment(url));
|
||||
}
|
||||
}
|
||||
|
||||
BasicNetworkCredential credentials = null;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(settings.Username))
|
||||
{
|
||||
credentials = new BasicNetworkCredential(settings.Username, settings.Password);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Send(email, settings.Server, settings.Port, settings.Ssl, credentials);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("Error sending email. Subject: {0}", email.Subject);
|
||||
_logger.Debug(ex, ex.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private void Send(MailMessage email, string server, int port, bool ssl, BasicNetworkCredential credentials)
|
||||
{
|
||||
var smtp = new SmtpClient(server, port);
|
||||
smtp.EnableSsl = ssl;
|
||||
smtp.Credentials = credentials;
|
||||
|
||||
smtp.Send(email);
|
||||
}
|
||||
|
||||
public ValidationFailure Test(EmailSettings settings)
|
||||
{
|
||||
const string body = "Success! You have properly configured your email notification settings";
|
||||
|
||||
try
|
||||
{
|
||||
SendEmail(settings, "Readarr - Test Notification", body);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex, "Unable to send test email");
|
||||
return new ValidationFailure("Server", "Unable to send test email");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue