|
|
@ -1,8 +1,9 @@
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
|
|
|
|
using System.Net.Mail;
|
|
|
|
|
|
|
|
using FluentValidation.Results;
|
|
|
|
using FluentValidation.Results;
|
|
|
|
|
|
|
|
using MailKit.Net.Smtp;
|
|
|
|
|
|
|
|
using MailKit.Security;
|
|
|
|
|
|
|
|
using MimeKit;
|
|
|
|
using NLog;
|
|
|
|
using NLog;
|
|
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Notifications.Email
|
|
|
|
namespace NzbDrone.Core.Notifications.Email
|
|
|
@ -24,27 +25,22 @@ namespace NzbDrone.Core.Notifications.Email
|
|
|
|
|
|
|
|
|
|
|
|
public void SendEmail(EmailSettings settings, string subject, string body, bool htmlBody = false)
|
|
|
|
public void SendEmail(EmailSettings settings, string subject, string body, bool htmlBody = false)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var email = new MailMessage();
|
|
|
|
var email = new MimeMessage();
|
|
|
|
email.From = new MailAddress(settings.From);
|
|
|
|
email.From.Add(MailboxAddress.Parse(settings.From));
|
|
|
|
|
|
|
|
|
|
|
|
settings.To.ToList().ForEach(x => email.To.Add(x));
|
|
|
|
settings.To.ToList().ForEach(x => email.To.Add(MailboxAddress.Parse(x)));
|
|
|
|
settings.CC.ToList().ForEach(x => email.CC.Add(x));
|
|
|
|
settings.CC.ToList().ForEach(x => email.Cc.Add(MailboxAddress.Parse(x)));
|
|
|
|
settings.Bcc.ToList().ForEach(x => email.Bcc.Add(x));
|
|
|
|
settings.Bcc.ToList().ForEach(x => email.Bcc.Add(MailboxAddress.Parse(x)));
|
|
|
|
|
|
|
|
|
|
|
|
email.Subject = subject;
|
|
|
|
email.Subject = subject;
|
|
|
|
email.Body = body;
|
|
|
|
email.Body = new TextPart(htmlBody ? "html" : "plain")
|
|
|
|
email.IsBodyHtml = htmlBody;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NetworkCredential credentials = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(settings.Username))
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
credentials = new NetworkCredential(settings.Username, settings.Password);
|
|
|
|
Text = body
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Send(email, settings.Server, settings.Port, settings.Ssl, credentials);
|
|
|
|
Send(email, settings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -54,13 +50,34 @@ namespace NzbDrone.Core.Notifications.Email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Send(MailMessage email, string server, int port, bool ssl, NetworkCredential credentials)
|
|
|
|
private void Send(MimeMessage email, EmailSettings settings)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var smtp = new SmtpClient(server, port);
|
|
|
|
using (var client = new SmtpClient())
|
|
|
|
smtp.EnableSsl = ssl;
|
|
|
|
{
|
|
|
|
smtp.Credentials = credentials;
|
|
|
|
var serverOption = SecureSocketOptions.Auto;
|
|
|
|
|
|
|
|
|
|
|
|
smtp.Send(email);
|
|
|
|
if (settings.RequireEncryption)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (settings.Port == 465)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
serverOption = SecureSocketOptions.SslOnConnect;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
serverOption = SecureSocketOptions.StartTls;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client.Connect(settings.Server, settings.Port, serverOption);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(settings.Username))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
client.Authenticate(settings.Username, settings.Password);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client.Send(email);
|
|
|
|
|
|
|
|
client.Disconnect(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ValidationFailure Test(EmailSettings settings)
|
|
|
|
public ValidationFailure Test(EmailSettings settings)
|
|
|
|