From 25a8640d8097ccf810baf07b5a96137894ff4b40 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 27 Aug 2019 09:23:03 -0400 Subject: [PATCH] get rid of old smtp mail delivery service --- src/Core/GlobalSettings.cs | 4 - .../SmtpMailDeliveryService.cs | 117 ------------------ 2 files changed, 121 deletions(-) delete mode 100644 src/Core/Services/Implementations/SmtpMailDeliveryService.cs diff --git a/src/Core/GlobalSettings.cs b/src/Core/GlobalSettings.cs index d4a4a0a369..4c933b894e 100644 --- a/src/Core/GlobalSettings.cs +++ b/src/Core/GlobalSettings.cs @@ -125,10 +125,6 @@ namespace Bit.Core public bool SslOverride { get; set; } = false; public string Username { get; set; } public string Password { get; set; } - [Obsolete] - public bool UseDefaultCredentials { get; set; } = false; - [Obsolete] - public string AuthType { get; set; } public bool TrustServer { get; set; } = false; } } diff --git a/src/Core/Services/Implementations/SmtpMailDeliveryService.cs b/src/Core/Services/Implementations/SmtpMailDeliveryService.cs deleted file mode 100644 index 3e70b37109..0000000000 --- a/src/Core/Services/Implementations/SmtpMailDeliveryService.cs +++ /dev/null @@ -1,117 +0,0 @@ -using System; -using System.Threading.Tasks; -using System.Net.Mail; -using System.Net; -using System.Text; -using Microsoft.Extensions.Logging; -using Bit.Core.Utilities; - -namespace Bit.Core.Services -{ - [Obsolete] - public class SmtpMailDeliveryService : IMailDeliveryService - { - private readonly GlobalSettings _globalSettings; - private readonly ILogger _logger; - private readonly string _replyDomain; - - public SmtpMailDeliveryService( - GlobalSettings globalSettings, - ILogger logger) - { - if(globalSettings.Mail?.Smtp?.Host == null) - { - throw new ArgumentNullException(nameof(globalSettings.Mail.Smtp.Host)); - } - if(globalSettings.Mail?.ReplyToEmail?.Contains("@") ?? false) - { - _replyDomain = globalSettings.Mail.ReplyToEmail.Split('@')[1]; - } - - _globalSettings = globalSettings; - _logger = logger; - } - - public Task SendEmailAsync(Models.Mail.MailMessage message) - { - var client = new SmtpClient(_globalSettings.Mail.Smtp.Host, _globalSettings.Mail.Smtp.Port); - client.EnableSsl = _globalSettings.Mail.Smtp.Ssl; - client.UseDefaultCredentials = _globalSettings.Mail.Smtp.UseDefaultCredentials; - client.DeliveryMethod = SmtpDeliveryMethod.Network; - - if(CoreHelpers.SettingHasValue(_globalSettings.Mail.Smtp.Username) && - CoreHelpers.SettingHasValue(_globalSettings.Mail.Smtp.Password)) - { - if(CoreHelpers.SettingHasValue(_globalSettings.Mail.Smtp.AuthType)) - { - var cred = new NetworkCredential(_globalSettings.Mail.Smtp.Username, - _globalSettings.Mail.Smtp.Password); - var cache = new CredentialCache(); - cache.Add(_globalSettings.Mail.Smtp.Host, _globalSettings.Mail.Smtp.Port, - _globalSettings.Mail.Smtp.AuthType, cred); - client.Credentials = cache; - } - else - { - client.Credentials = new NetworkCredential(_globalSettings.Mail.Smtp.Username, - _globalSettings.Mail.Smtp.Password); - } - } - - var smtpMessage = new MailMessage(); - smtpMessage.From = new MailAddress(_globalSettings.Mail.ReplyToEmail, _globalSettings.SiteName); - smtpMessage.Subject = message.Subject; - smtpMessage.SubjectEncoding = Encoding.UTF8; - smtpMessage.BodyEncoding = Encoding.UTF8; - smtpMessage.BodyTransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable; - if(!string.IsNullOrWhiteSpace(_replyDomain)) - { - smtpMessage.Headers.Add("Message-ID", $"<{Guid.NewGuid()}@{_replyDomain}>"); - } - foreach(var address in message.ToEmails) - { - smtpMessage.To.Add(new MailAddress(address)); - } - - if(message.BccEmails != null) - { - foreach(var address in message.BccEmails) - { - smtpMessage.Bcc.Add(new MailAddress(address)); - } - } - - if(string.IsNullOrWhiteSpace(message.TextContent)) - { - smtpMessage.IsBodyHtml = true; - smtpMessage.Body = message.HtmlContent; - } - else - { - smtpMessage.Body = message.TextContent; - var htmlView = AlternateView.CreateAlternateViewFromString(message.HtmlContent); - htmlView.ContentType = new System.Net.Mime.ContentType("text/html"); - smtpMessage.AlternateViews.Add(htmlView); - } - - client.SendCompleted += (sender, e) => - { - if(e.Error != null) - { - _logger.LogError(e.Error, "Mail send failed."); - } - - if(e.Cancelled) - { - _logger.LogWarning("Mail send canceled."); - } - - smtpMessage.Dispose(); - client.Dispose(); - }; - - client.SendAsync(smtpMessage, null); - return Task.FromResult(0); - } - } -}