New: Replace SmtpClient with Mailkit

Closes #4213
This commit is contained in:
Qstick 2021-01-13 19:58:22 -05:00 committed by GitHub
parent 8a511a0e20
commit 579c443349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 24 deletions

View File

@ -1,7 +1,8 @@
using System; using System;
using System.Net; using System.Linq;
using System.Net.Mail;
using FluentValidation.Results; using FluentValidation.Results;
using MailKit.Net.Smtp;
using MimeKit;
using NLog; using NLog;
namespace NzbDrone.Core.Notifications.Email namespace NzbDrone.Core.Notifications.Email
@ -23,23 +24,20 @@ 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));
email.To.Add(MailboxAddress.Parse(settings.To));
email.To.Add(settings.To);
email.Subject = subject; email.Subject = subject;
email.Body = body; email.Body = new TextPart(htmlBody ? "html" : "plain")
email.IsBodyHtml = htmlBody; {
Text = body
NetworkCredential credentials = null; };
if (!string.IsNullOrWhiteSpace(settings.Username))
credentials = new NetworkCredential(settings.Username, settings.Password);
try try
{ {
Send(email, settings.Server, settings.Port, settings.Ssl, credentials); Send(email, settings);
} }
catch(Exception ex) catch(Exception ex)
{ {
@ -49,13 +47,20 @@ 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; client.Connect(settings.Server, settings.Port);
smtp.Send(email); 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)

View File

@ -31,9 +31,6 @@ namespace NzbDrone.Core.Notifications.Email
[FieldDefinition(1, Label = "Port")] [FieldDefinition(1, Label = "Port")]
public int Port { get; set; } public int Port { get; set; }
[FieldDefinition(2, Label = "SSL", Type = FieldType.Checkbox)]
public bool Ssl { get; set; }
[FieldDefinition(3, Label = "Username", Privacy = PrivacyLevel.UserName)] [FieldDefinition(3, Label = "Username", Privacy = PrivacyLevel.UserName)]
public string Username { get; set; } public string Username { get; set; }

View File

@ -8,6 +8,7 @@
<PackageReference Include="FluentValidation" Version="8.4.0" /> <PackageReference Include="FluentValidation" Version="8.4.0" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0007" /> <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0007" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" /> <PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="MailKit" Version="2.10.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="NLog" Version="4.6.6" /> <PackageReference Include="NLog" Version="4.6.6" />
<PackageReference Include="OAuth" Version="1.0.3" /> <PackageReference Include="OAuth" Version="1.0.3" />