mirror of
https://github.com/Radarr/Radarr.git
synced 2024-08-18 16:29:41 +02:00
Replace SmtpClient with Mailkit
This commit is contained in:
parent
1e396ead5e
commit
2013e1f580
@ -25,7 +25,6 @@ public void Setup()
|
||||
_emailSettings = Builder<EmailSettings>.CreateNew()
|
||||
.With(s => s.Server = "someserver")
|
||||
.With(s => s.Port = 567)
|
||||
.With(s => s.Ssl = true)
|
||||
.With(s => s.From = "radarr@radarr.video")
|
||||
.With(s => s.To = new string[] { "radarr@radarr.video" })
|
||||
.Build();
|
||||
|
@ -1,8 +1,9 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using FluentValidation.Results;
|
||||
using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
using MimeKit;
|
||||
using NLog;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Email
|
||||
@ -24,27 +25,22 @@ public EmailService(Logger logger)
|
||||
|
||||
public void SendEmail(EmailSettings settings, string subject, string body, bool htmlBody = false)
|
||||
{
|
||||
var email = new MailMessage();
|
||||
email.From = new MailAddress(settings.From);
|
||||
var email = new MimeMessage();
|
||||
email.From.Add(MailboxAddress.Parse(settings.From));
|
||||
|
||||
settings.To.ToList().ForEach(x => email.To.Add(x));
|
||||
settings.CC.ToList().ForEach(x => email.CC.Add(x));
|
||||
settings.Bcc.ToList().ForEach(x => email.Bcc.Add(x));
|
||||
settings.To.ToList().ForEach(x => email.To.Add(MailboxAddress.Parse(x)));
|
||||
settings.CC.ToList().ForEach(x => email.Cc.Add(MailboxAddress.Parse(x)));
|
||||
settings.Bcc.ToList().ForEach(x => email.Bcc.Add(MailboxAddress.Parse(x)));
|
||||
|
||||
email.Subject = subject;
|
||||
email.Body = body;
|
||||
email.IsBodyHtml = htmlBody;
|
||||
|
||||
NetworkCredential credentials = null;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(settings.Username))
|
||||
email.Body = new TextPart(htmlBody ? "html" : "plain")
|
||||
{
|
||||
credentials = new NetworkCredential(settings.Username, settings.Password);
|
||||
}
|
||||
Text = body
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
Send(email, settings.Server, settings.Port, settings.Ssl, credentials);
|
||||
Send(email, settings);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -54,13 +50,34 @@ public void SendEmail(EmailSettings settings, string subject, string body, bool
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
smtp.EnableSsl = ssl;
|
||||
smtp.Credentials = credentials;
|
||||
using (var client = new SmtpClient())
|
||||
{
|
||||
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)
|
||||
|
@ -34,7 +34,6 @@ public EmailSettings()
|
||||
{
|
||||
Server = "smtp.gmail.com";
|
||||
Port = 587;
|
||||
Ssl = true;
|
||||
|
||||
To = Array.Empty<string>();
|
||||
CC = Array.Empty<string>();
|
||||
@ -47,8 +46,8 @@ public EmailSettings()
|
||||
[FieldDefinition(1, Label = "Port")]
|
||||
public int Port { get; set; }
|
||||
|
||||
[FieldDefinition(2, Label = "SSL", Type = FieldType.Checkbox)]
|
||||
public bool Ssl { get; set; }
|
||||
[FieldDefinition(2, Label = "Require Encryption", HelpText = "Require SSL (Port 465 only) or StartTLS (any other port)", Type = FieldType.Checkbox)]
|
||||
public bool RequireEncryption { get; set; }
|
||||
|
||||
[FieldDefinition(3, Label = "Username", Privacy = PrivacyLevel.UserName)]
|
||||
public string Username { get; set; }
|
||||
|
@ -4,6 +4,7 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.0.30" />
|
||||
<PackageReference Include="MailKit" Version="2.10.1" />
|
||||
<PackageReference Include="System.Memory" Version="4.5.4" />
|
||||
<PackageReference Include="System.ServiceModel.Syndication" Version="5.0.0" />
|
||||
<PackageReference Include="FluentMigrator.Runner" Version="4.0.0-alpha.268" />
|
||||
|
Loading…
Reference in New Issue
Block a user