Added SMTP as an ExternalNotifcation method.

pull/3113/head
Mark McDowall 13 years ago
parent 955c5ce6d8
commit 9957aef811

@ -105,6 +105,7 @@ namespace NzbDrone.Core
private static void BindExternalNotifications() private static void BindExternalNotifications()
{ {
_kernel.Bind<ExternalNotificationBase>().To<Xbmc>(); _kernel.Bind<ExternalNotificationBase>().To<Xbmc>();
_kernel.Bind<ExternalNotificationBase>().To<Smtp>();
var notifiers = _kernel.GetAll<ExternalNotificationBase>(); var notifiers = _kernel.GetAll<ExternalNotificationBase>();
_kernel.Get<ExternalNotificationProvider>().InitializeNotifiers(notifiers.ToList()); _kernel.Get<ExternalNotificationProvider>().InitializeNotifiers(notifiers.ToList());

@ -215,6 +215,7 @@
<Compile Include="Providers\EnviromentProvider.cs" /> <Compile Include="Providers\EnviromentProvider.cs" />
<Compile Include="Providers\Core\ConfigFileProvider.cs" /> <Compile Include="Providers\Core\ConfigFileProvider.cs" />
<Compile Include="Providers\Core\UdpProvider.cs" /> <Compile Include="Providers\Core\UdpProvider.cs" />
<Compile Include="Providers\ExternalNotification\Smtp.cs" />
<Compile Include="Providers\Jobs\BacklogSearchJob.cs" /> <Compile Include="Providers\Jobs\BacklogSearchJob.cs" />
<Compile Include="Providers\Jobs\BannerDownloadJob.cs" /> <Compile Include="Providers\Jobs\BannerDownloadJob.cs" />
<Compile Include="Providers\Jobs\ConvertEpisodeJob.cs" /> <Compile Include="Providers\Jobs\ConvertEpisodeJob.cs" />
@ -226,6 +227,7 @@
<Compile Include="Providers\PostDownloadProvider.cs" /> <Compile Include="Providers\PostDownloadProvider.cs" />
<Compile Include="Providers\QualityTypeProvider.cs" /> <Compile Include="Providers\QualityTypeProvider.cs" />
<Compile Include="Providers\SearchProvider.cs" /> <Compile Include="Providers\SearchProvider.cs" />
<Compile Include="Providers\SmtpProvider.cs" />
<Compile Include="Providers\UpdateProvider.cs" /> <Compile Include="Providers\UpdateProvider.cs" />
<Compile Include="Providers\Xbmc\ResourceManager.cs" /> <Compile Include="Providers\Xbmc\ResourceManager.cs" />
<Compile Include="Model\Xbmc\TvShowResult.cs" /> <Compile Include="Model\Xbmc\TvShowResult.cs" />

@ -255,6 +255,63 @@ namespace NzbDrone.Core.Providers.Core
set { SetValue("UpdateUrl", value); } set { SetValue("UpdateUrl", value); }
} }
public virtual Boolean SmtpNotifyOnGrab
{
get { return GetValueBoolean("SmtpNotifyOnGrab"); }
set { SetValue("SmtpNotifyOnGrab", value); }
}
public virtual Boolean SmtpNotifyOnDownload
{
get { return GetValueBoolean("SmtpNotifyOnDownload"); }
set { SetValue("SmtpNotifyOnDownload", value); }
}
public virtual string SmtpServer
{
get { return GetValue("SmtpServer", String.Empty); }
set { SetValue("SmtpServer", value); }
}
public virtual int SmtpPort
{
get { return GetValueInt("SmtpPort", 25); }
set { SetValue("SmtpPort", value); }
}
public virtual Boolean SmtpUseSsl
{
get { return GetValueBoolean("SmtpUseSsl"); }
set { SetValue("SmtpUseSsl", value); }
}
public virtual string SmtpUsername
{
get { return GetValue("SmtpUsername", String.Empty); }
set { SetValue("SmtpUsername", value); }
}
public virtual string SmtpPassword
{
get { return GetValue("SmtpPassword", String.Empty); }
set { SetValue("SmtpPassword", value); }
}
public virtual string SmtpFromAddress
{
get { return GetValue("SmtpFromAddress", String.Empty); }
set { SetValue("SmtpFromAddress", value); }
}
public virtual string SmtpToAddresses
{
get { return GetValue("SmtpToAddresses", String.Empty); }
set { SetValue("SmtpToAddresses", value); }
}
private string GetValue(string key) private string GetValue(string key)
{ {
return GetValue(key, String.Empty); return GetValue(key, String.Empty);

@ -0,0 +1,51 @@
using System;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Providers.ExternalNotification
{
public class Smtp: ExternalNotificationBase
{
private readonly SmtpProvider _smtpProvider;
public Smtp(ConfigProvider configProvider, SmtpProvider smtpProvider)
: base(configProvider)
{
_smtpProvider = smtpProvider;
}
public override string Name
{
get { return "SMTP"; }
}
public override void OnGrab(string message)
{
const string subject = "NzbDrone [TV] - Grabbed";
var body = String.Format("{0} sent to SABnzbd queue.", message);
if (_configProvider.SmtpNotifyOnGrab)
{
_logger.Trace("Sending SMTP Notification");
_smtpProvider.SendEmail(subject, body);
}
}
public override void OnDownload(string message, Series series)
{
const string subject = "NzbDrone [TV] - Downloaded";
var body = String.Format("{0} Downloaded and sorted.", message);
if (_configProvider.SmtpNotifyOnDownload)
{
_logger.Trace("Sending SMTP Notification");
_smtpProvider.SendEmail(subject, body);
}
}
public override void OnRename(string message, Series series)
{
}
}
}

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using NLog;
using Ninject;
using NzbDrone.Core.Providers.Core;
namespace NzbDrone.Core.Providers
{
public class SmtpProvider
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly ConfigProvider _configProvider;
[Inject]
public SmtpProvider(ConfigProvider configProvider)
{
_configProvider = configProvider;
}
public virtual bool SendEmail(string subject, string body, bool htmlBody = false)
{
try
{
//Create the Email message
var email = new MailMessage();
//Set the addresses
email.From = new MailAddress(_configProvider.SmtpFromAddress);
//Allow multiple to addresses (split on each comma)
foreach (var toAddress in _configProvider.SmtpToAddresses.Split(','))
{
email.To.Add(toAddress.Trim());
}
//Set the Subject
email.Subject = subject;
//Set the Body
email.Body = body;
//Html Body
email.IsBodyHtml = htmlBody;
//Create the SMTP connection
var smtp = new SmtpClient(_configProvider.SmtpServer, _configProvider.SmtpPort);
//Enable SSL
smtp.EnableSsl = true;
//Credentials
smtp.Credentials = new System.Net.NetworkCredential(_configProvider.SmtpUsername, _configProvider.SmtpPassword);
//Send the email
smtp.Send(email);
return true;
}
catch (Exception ex)
{
Logger.Error("There was an error sending an email.");
Logger.TraceException(ex.Message, ex);
}
return false;
}
}
}
Loading…
Cancel
Save