2011-10-26 04:25:08 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2011-10-27 07:46:54 +02:00
|
|
|
|
using System.Net;
|
2011-10-26 04:25:08 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 07:36:49 +02:00
|
|
|
|
public virtual void SendEmail(string subject, string body, bool htmlBody = false)
|
2011-10-26 04:25:08 +02:00
|
|
|
|
{
|
2011-10-27 07:46:54 +02:00
|
|
|
|
//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(','))
|
2011-10-26 04:25:08 +02:00
|
|
|
|
{
|
2011-10-27 07:46:54 +02:00
|
|
|
|
email.To.Add(toAddress.Trim());
|
|
|
|
|
}
|
2011-10-26 04:25:08 +02:00
|
|
|
|
|
2011-10-27 07:46:54 +02:00
|
|
|
|
//Set the Subject
|
|
|
|
|
email.Subject = subject;
|
2011-10-26 04:25:08 +02:00
|
|
|
|
|
2011-10-27 07:46:54 +02:00
|
|
|
|
//Set the Body
|
|
|
|
|
email.Body = body;
|
2011-10-26 04:25:08 +02:00
|
|
|
|
|
2011-10-27 07:46:54 +02:00
|
|
|
|
//Html Body
|
|
|
|
|
email.IsBodyHtml = htmlBody;
|
2011-10-26 04:25:08 +02:00
|
|
|
|
|
2011-10-27 07:46:54 +02:00
|
|
|
|
//Handle credentials
|
|
|
|
|
var username = _configProvider.SmtpUsername;
|
|
|
|
|
var password = _configProvider.SmtpPassword;
|
2011-10-26 04:25:08 +02:00
|
|
|
|
|
2011-10-27 07:46:54 +02:00
|
|
|
|
NetworkCredential credentials = null;
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(username))
|
|
|
|
|
credentials = new NetworkCredential(username, password);
|
|
|
|
|
|
|
|
|
|
//Send the email
|
2012-04-30 18:42:07 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Send(email, _configProvider.SmtpServer, _configProvider.SmtpPort, _configProvider.SmtpUseSsl, credentials);
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Error sending email. Subject: {0}", email.Subject);
|
|
|
|
|
}
|
2011-10-27 07:46:54 +02:00
|
|
|
|
}
|
2011-10-26 04:25:08 +02:00
|
|
|
|
|
2011-10-27 07:46:54 +02:00
|
|
|
|
public virtual bool SendTestEmail(string server, int port, bool ssl, string username, string password, string fromAddress, string toAddresses)
|
|
|
|
|
{
|
|
|
|
|
var subject = "NzbDrone SMTP Test Notification";
|
|
|
|
|
var body = "This is a test email from NzbDrone, if you received this message you properly configured your SMTP settings! (Now save them!)";
|
|
|
|
|
|
|
|
|
|
//Create the Email message
|
|
|
|
|
var email = new MailMessage();
|
|
|
|
|
|
|
|
|
|
//Set the addresses
|
|
|
|
|
email.From = new MailAddress(fromAddress);
|
|
|
|
|
|
|
|
|
|
//Allow multiple to addresses (split on each comma)
|
|
|
|
|
foreach (var toAddress in toAddresses.Split(','))
|
|
|
|
|
{
|
|
|
|
|
email.To.Add(toAddress.Trim());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Set the Subject
|
|
|
|
|
email.Subject = subject;
|
|
|
|
|
|
|
|
|
|
//Set the Body
|
|
|
|
|
email.Body = body;
|
|
|
|
|
|
|
|
|
|
//Html Body
|
|
|
|
|
email.IsBodyHtml = false;
|
|
|
|
|
|
|
|
|
|
//Handle credentials
|
|
|
|
|
NetworkCredential credentials = null;
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(username))
|
|
|
|
|
credentials = new NetworkCredential(username, password);
|
|
|
|
|
|
|
|
|
|
//Send the email
|
2012-04-30 07:36:49 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Send(email, server, port, ssl, credentials);
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.TraceException("Failed to send test email", ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2011-10-27 07:46:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 07:36:49 +02:00
|
|
|
|
public virtual void Send(MailMessage email, string server, int port, bool ssl, NetworkCredential credentials)
|
2011-10-27 07:46:54 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2011-10-26 04:25:08 +02:00
|
|
|
|
//Create the SMTP connection
|
2011-10-27 07:46:54 +02:00
|
|
|
|
var smtp = new SmtpClient(server, port);
|
2011-10-26 04:25:08 +02:00
|
|
|
|
|
|
|
|
|
//Enable SSL
|
2011-10-27 07:46:54 +02:00
|
|
|
|
smtp.EnableSsl = ssl;
|
2011-10-26 04:25:08 +02:00
|
|
|
|
|
|
|
|
|
//Credentials
|
2011-10-27 07:46:54 +02:00
|
|
|
|
smtp.Credentials = credentials;
|
2011-10-26 04:25:08 +02:00
|
|
|
|
|
|
|
|
|
//Send the email
|
|
|
|
|
smtp.Send(email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2012-04-30 07:36:49 +02:00
|
|
|
|
Logger.ErrorException("There was an error sending an email.", ex);
|
|
|
|
|
throw;
|
2011-10-26 04:25:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|