From 419ec2bdb94c1078c1a95eed7a300c50e0f3fe94 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 15 Jan 2021 17:29:38 -0800 Subject: [PATCH] New: Require Encryption option for email Fixes #1908 (cherry picked from commit e704ee617f379ff61fb1bf9fb4044d6b4eb12814) --- .../Notifications/Email/EmailService.cs | 17 ++++++++++++++++- .../Notifications/Email/EmailSettings.cs | 5 ++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Core/Notifications/Email/EmailService.cs b/src/NzbDrone.Core/Notifications/Email/EmailService.cs index a9f195cb0..b0cfb1067 100644 --- a/src/NzbDrone.Core/Notifications/Email/EmailService.cs +++ b/src/NzbDrone.Core/Notifications/Email/EmailService.cs @@ -2,6 +2,7 @@ using System; using System.Linq; using FluentValidation.Results; using MailKit.Net.Smtp; +using MailKit.Security; using MimeKit; using NLog; @@ -51,7 +52,21 @@ namespace NzbDrone.Core.Notifications.Email { using (var client = new SmtpClient()) { - client.Connect(settings.Server, settings.Port); + var serverOption = SecureSocketOptions.Auto; + + 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)) { diff --git a/src/NzbDrone.Core/Notifications/Email/EmailSettings.cs b/src/NzbDrone.Core/Notifications/Email/EmailSettings.cs index 5ec3e008c..0ee52dafe 100644 --- a/src/NzbDrone.Core/Notifications/Email/EmailSettings.cs +++ b/src/NzbDrone.Core/Notifications/Email/EmailSettings.cs @@ -22,7 +22,7 @@ namespace NzbDrone.Core.Notifications.Email public EmailSettings() { - Port = 25; + Port = 587; } [FieldDefinition(0, Label = "Server", HelpText = "Hostname or IP of Email server")] @@ -31,6 +31,9 @@ namespace NzbDrone.Core.Notifications.Email [FieldDefinition(1, Label = "Port")] public int Port { 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; }