2013-05-20 01:17:32 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Mail;
|
|
|
|
|
using NLog;
|
2013-06-13 08:41:26 +02:00
|
|
|
|
using NzbDrone.Common.Messaging;
|
|
|
|
|
using Omu.ValueInjecter;
|
2013-05-20 01:17:32 +02:00
|
|
|
|
|
2013-05-27 07:44:54 +02:00
|
|
|
|
namespace NzbDrone.Core.Notifications.Email
|
2013-05-20 01:17:32 +02:00
|
|
|
|
{
|
2013-06-13 08:41:26 +02:00
|
|
|
|
public interface IEmailService
|
|
|
|
|
{
|
|
|
|
|
void SendEmail(EmailSettings settings, string subject, string body, bool htmlBody = false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class EmailService : IEmailService, IExecute<TestEmailCommand>
|
2013-05-20 01:17:32 +02:00
|
|
|
|
{
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-06-13 08:41:26 +02:00
|
|
|
|
public EmailService(Logger logger)
|
2013-05-20 01:17:32 +02:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-13 08:41:26 +02:00
|
|
|
|
public void SendEmail(EmailSettings settings, string subject, string body, bool htmlBody = false)
|
2013-05-20 01:17:32 +02:00
|
|
|
|
{
|
|
|
|
|
var email = new MailMessage();
|
|
|
|
|
email.From = new MailAddress(settings.From);
|
|
|
|
|
|
|
|
|
|
email.To.Add(settings.To);
|
|
|
|
|
|
|
|
|
|
email.Subject = subject;
|
|
|
|
|
email.Body = body;
|
|
|
|
|
email.IsBodyHtml = htmlBody;
|
|
|
|
|
|
|
|
|
|
NetworkCredential credentials = null;
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(settings.Username))
|
|
|
|
|
credentials = new NetworkCredential(settings.Username, settings.Password);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-06-13 08:41:26 +02:00
|
|
|
|
Send(email, settings.Server, settings.Port, settings.Ssl, credentials);
|
2013-05-20 01:17:32 +02:00
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("Error sending email. Subject: {0}", email.Subject);
|
|
|
|
|
_logger.TraceException(ex.Message, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-13 08:41:26 +02:00
|
|
|
|
private void Send(MailMessage email, string server, int port, bool ssl, NetworkCredential credentials)
|
2013-05-20 01:17:32 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var smtp = new SmtpClient(server, port);
|
|
|
|
|
smtp.EnableSsl = ssl;
|
|
|
|
|
smtp.Credentials = credentials;
|
|
|
|
|
|
|
|
|
|
smtp.Send(email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("There was an error sending an email.", ex);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-13 08:41:26 +02:00
|
|
|
|
|
|
|
|
|
public void Execute(TestEmailCommand message)
|
|
|
|
|
{
|
|
|
|
|
var settings = new EmailSettings();
|
|
|
|
|
settings.InjectFrom(message);
|
|
|
|
|
|
2013-07-16 02:46:41 +02:00
|
|
|
|
const string body = "Success! You have properly configured your email notification settings";
|
2013-06-13 08:41:26 +02:00
|
|
|
|
|
|
|
|
|
SendEmail(settings, "NzbDrone - Test Notification", body);
|
|
|
|
|
}
|
2013-05-20 01:17:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|