sonarr-repo-only/NzbDrone.Core/Notifications/Email/EmailProvider.cs

65 lines
1.7 KiB
C#
Raw Normal View History

2013-05-19 23:17:32 +00:00
using System;
using System.Net;
using System.Net.Mail;
using NLog;
2013-05-27 05:44:54 +00:00
namespace NzbDrone.Core.Notifications.Email
2013-05-19 23:17:32 +00:00
{
2013-05-27 05:44:54 +00:00
public class EmailProvider
2013-05-19 23:17:32 +00:00
{
private readonly Logger _logger;
2013-05-27 05:44:54 +00:00
public EmailProvider(Logger logger)
2013-05-19 23:17:32 +00:00
{
_logger = logger;
}
2013-05-27 05:44:54 +00:00
public virtual void SendEmail(EmailSettings settings, string subject, string body, bool htmlBody = false)
2013-05-19 23:17:32 +00: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
{
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;
}
}
}
}