mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-14 18:57:39 -07:00
Renamed Smtp to Email
This commit is contained in:
parent
d8259f5cff
commit
0d21f34ec5
5 changed files with 24 additions and 31 deletions
40
NzbDrone.Core/Notifications/Email/Email.cs
Normal file
40
NzbDrone.Core/Notifications/Email/Email.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
using System;
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Email
|
||||
{
|
||||
public class Email : NotificationBase<EmailSettings>
|
||||
{
|
||||
private readonly EmailProvider _smtpProvider;
|
||||
|
||||
public Email(EmailProvider smtpProvider)
|
||||
{
|
||||
_smtpProvider = smtpProvider;
|
||||
}
|
||||
|
||||
public override string Name
|
||||
{
|
||||
get { return "Email"; }
|
||||
}
|
||||
|
||||
public override void OnGrab(string message)
|
||||
{
|
||||
const string subject = "NzbDrone [TV] - Grabbed";
|
||||
var body = String.Format("{0} sent to SABnzbd queue.", message);
|
||||
|
||||
_smtpProvider.SendEmail(Settings, 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);
|
||||
|
||||
_smtpProvider.SendEmail(Settings, subject, body);
|
||||
}
|
||||
|
||||
public override void AfterRename(Series series)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
64
NzbDrone.Core/Notifications/Email/EmailProvider.cs
Normal file
64
NzbDrone.Core/Notifications/Email/EmailProvider.cs
Normal file
|
@ -0,0 +1,64 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using NLog;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Email
|
||||
{
|
||||
public class EmailProvider
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public EmailProvider(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public virtual void SendEmail(EmailSettings settings, string subject, string body, bool htmlBody = false)
|
||||
{
|
||||
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
|
||||
{
|
||||
Send(email, settings.Server, settings.Port, settings.UseSsl, credentials);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
_logger.Error("Error sending email. Subject: {0}", email.Subject);
|
||||
_logger.TraceException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Send(MailMessage email, string server, int port, bool ssl, NetworkCredential credentials)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
37
NzbDrone.Core/Notifications/Email/EmailSettings.cs
Normal file
37
NzbDrone.Core/Notifications/Email/EmailSettings.cs
Normal file
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
using NzbDrone.Core.Annotations;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Email
|
||||
{
|
||||
public class EmailSettings : INotifcationSettings
|
||||
{
|
||||
[FieldDefinition(0, Label = "Server", HelpText = "Hostname or IP of Email server")]
|
||||
public String Server { get; set; }
|
||||
|
||||
[FieldDefinition(1, Label = "Port")]
|
||||
public Int32 Port { get; set; }
|
||||
|
||||
[FieldDefinition(2, Label = "Use SSL", HelpText = "Does your Email server use SSL?")]
|
||||
public Boolean UseSsl { get; set; }
|
||||
|
||||
[FieldDefinition(3, Label = "Username")]
|
||||
public String Username { get; set; }
|
||||
|
||||
[FieldDefinition(4, Label = "Password")]
|
||||
public String Password { get; set; }
|
||||
|
||||
[FieldDefinition(5, Label = "Sender Address")]
|
||||
public String From { get; set; }
|
||||
|
||||
[FieldDefinition(6, Label = "Recipient Address")]
|
||||
public String To { get; set; }
|
||||
|
||||
public bool IsValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(Server) && Port > 0 && !string.IsNullOrWhiteSpace(From) && !string.IsNullOrWhiteSpace(To);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue