2011-10-26 05:25:08 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2011-10-27 08:46:54 +03:00
|
|
|
using System.Net;
|
2011-10-26 05:25:08 +03:00
|
|
|
using System.Net.Mail;
|
|
|
|
using System.Text;
|
|
|
|
using NLog;
|
2013-02-24 09:48:52 +03:00
|
|
|
using NzbDrone.Core.Configuration;
|
2011-10-26 05:25:08 +03:00
|
|
|
using NzbDrone.Core.Providers.Core;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
|
|
|
public class SmtpProvider
|
|
|
|
{
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2013-02-24 09:48:52 +03:00
|
|
|
private readonly IConfigService _configService;
|
2011-10-26 05:25:08 +03:00
|
|
|
|
2013-02-24 09:48:52 +03:00
|
|
|
public SmtpProvider(IConfigService configService)
|
2011-10-26 05:25:08 +03:00
|
|
|
{
|
2013-02-24 09:48:52 +03:00
|
|
|
_configService = configService;
|
2011-10-26 05:25:08 +03:00
|
|
|
}
|
|
|
|
|
2012-04-30 08:36:49 +03:00
|
|
|
public virtual void SendEmail(string subject, string body, bool htmlBody = false)
|
2011-10-26 05:25:08 +03:00
|
|
|
{
|
2011-10-27 08:46:54 +03:00
|
|
|
//Create the Email message
|
|
|
|
var email = new MailMessage();
|
|
|
|
|
|
|
|
//Set the addresses
|
2013-02-24 09:48:52 +03:00
|
|
|
email.From = new MailAddress(_configService.SmtpFromAddress);
|
2011-10-27 08:46:54 +03:00
|
|
|
|
|
|
|
//Allow multiple to addresses (split on each comma)
|
2013-02-24 09:48:52 +03:00
|
|
|
foreach (var toAddress in _configService.SmtpToAddresses.Split(','))
|
2011-10-26 05:25:08 +03:00
|
|
|
{
|
2011-10-27 08:46:54 +03:00
|
|
|
email.To.Add(toAddress.Trim());
|
|
|
|
}
|
2011-10-26 05:25:08 +03:00
|
|
|
|
2011-10-27 08:46:54 +03:00
|
|
|
//Set the Subject
|
|
|
|
email.Subject = subject;
|
2011-10-26 05:25:08 +03:00
|
|
|
|
2011-10-27 08:46:54 +03:00
|
|
|
//Set the Body
|
|
|
|
email.Body = body;
|
2011-10-26 05:25:08 +03:00
|
|
|
|
2011-10-27 08:46:54 +03:00
|
|
|
//Html Body
|
|
|
|
email.IsBodyHtml = htmlBody;
|
2011-10-26 05:25:08 +03:00
|
|
|
|
2011-10-27 08:46:54 +03:00
|
|
|
//Handle credentials
|
2013-02-24 09:48:52 +03:00
|
|
|
var username = _configService.SmtpUsername;
|
|
|
|
var password = _configService.SmtpPassword;
|
2011-10-26 05:25:08 +03:00
|
|
|
|
2011-10-27 08:46:54 +03:00
|
|
|
NetworkCredential credentials = null;
|
|
|
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(username))
|
|
|
|
credentials = new NetworkCredential(username, password);
|
|
|
|
|
|
|
|
//Send the email
|
2012-04-30 19:42:07 +03:00
|
|
|
try
|
|
|
|
{
|
2013-02-24 09:48:52 +03:00
|
|
|
Send(email, _configService.SmtpServer, _configService.SmtpPort, _configService.SmtpUseSsl, credentials);
|
2012-04-30 19:42:07 +03:00
|
|
|
}
|
|
|
|
catch(Exception ex)
|
|
|
|
{
|
|
|
|
Logger.Error("Error sending email. Subject: {0}", email.Subject);
|
2012-11-21 19:14:57 +03:00
|
|
|
Logger.TraceException(ex.Message, ex);
|
2012-04-30 19:42:07 +03:00
|
|
|
}
|
2011-10-27 08:46:54 +03:00
|
|
|
}
|
2011-10-26 05:25:08 +03:00
|
|
|
|
2011-10-27 08:46:54 +03: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 08:36:49 +03: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 08:46:54 +03:00
|
|
|
}
|
|
|
|
|
2012-04-30 08:36:49 +03:00
|
|
|
public virtual void Send(MailMessage email, string server, int port, bool ssl, NetworkCredential credentials)
|
2011-10-27 08:46:54 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2011-10-26 05:25:08 +03:00
|
|
|
//Create the SMTP connection
|
2011-10-27 08:46:54 +03:00
|
|
|
var smtp = new SmtpClient(server, port);
|
2011-10-26 05:25:08 +03:00
|
|
|
|
|
|
|
//Enable SSL
|
2011-10-27 08:46:54 +03:00
|
|
|
smtp.EnableSsl = ssl;
|
2011-10-26 05:25:08 +03:00
|
|
|
|
|
|
|
//Credentials
|
2011-10-27 08:46:54 +03:00
|
|
|
smtp.Credentials = credentials;
|
2011-10-26 05:25:08 +03:00
|
|
|
|
|
|
|
//Send the email
|
|
|
|
smtp.Send(email);
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2012-04-30 08:36:49 +03:00
|
|
|
Logger.ErrorException("There was an error sending an email.", ex);
|
|
|
|
throw;
|
2011-10-26 05:25:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|