From 0ee863c1d0cda0bd6ee3fccfb8c8cb5978847a72 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 30 May 2017 22:40:02 -0400 Subject: [PATCH] SmtpMailDeliveryService --- .gitignore | 3 +- src/Core/GlobalSettings.cs | 12 ++++- .../SendGridMailDeliveryService.cs | 7 ++- .../SmtpMailDeliveryService.cs | 52 +++++++++++++++++-- 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 5cc8dc3d1..ea045d05a 100644 --- a/.gitignore +++ b/.gitignore @@ -201,4 +201,5 @@ project.lock.json *.jfm mail_dist/ *.refactorlog -*.scmp \ No newline at end of file +*.scmp +src/Core/Properties/launchSettings.json \ No newline at end of file diff --git a/src/Core/GlobalSettings.cs b/src/Core/GlobalSettings.cs index 0c93c9e41..35aa1aded 100644 --- a/src/Core/GlobalSettings.cs +++ b/src/Core/GlobalSettings.cs @@ -27,8 +27,18 @@ public class MailSettings { - public string ApiKey { get; set; } public string ReplyToEmail { get; set; } + public string SendGridApiKey { get; set; } + public SmtpSettings Smtp { get; set; } = new SmtpSettings(); + + public class SmtpSettings + { + public string Host { get; set; } + public int Port { get; set; } + public bool Ssl { get; set; } + public string Username { get; set; } + public string Password { get; set; } + } } public class PushSettings diff --git a/src/Core/Services/Implementations/SendGridMailDeliveryService.cs b/src/Core/Services/Implementations/SendGridMailDeliveryService.cs index 3dc5570a3..683e1f6a3 100644 --- a/src/Core/Services/Implementations/SendGridMailDeliveryService.cs +++ b/src/Core/Services/Implementations/SendGridMailDeliveryService.cs @@ -15,8 +15,13 @@ namespace Bit.Core.Services public SendGridMailDeliveryService(GlobalSettings globalSettings) { + if(globalSettings.Mail?.SendGridApiKey == null) + { + throw new ArgumentNullException(nameof(globalSettings.Mail.SendGridApiKey)); + } + _globalSettings = globalSettings; - _client = new SendGridClient(_globalSettings.Mail.ApiKey); + _client = new SendGridClient(_globalSettings.Mail.SendGridApiKey); } public async Task SendEmailAsync(MailMessage message) diff --git a/src/Core/Services/Implementations/SmtpMailDeliveryService.cs b/src/Core/Services/Implementations/SmtpMailDeliveryService.cs index 0f368b5fe..c63c79538 100644 --- a/src/Core/Services/Implementations/SmtpMailDeliveryService.cs +++ b/src/Core/Services/Implementations/SmtpMailDeliveryService.cs @@ -1,6 +1,8 @@ using System; using System.Threading.Tasks; -using Bit.Core.Models.Mail; +using System.Net.Mail; +using System.Net; +using System.Text; namespace Bit.Core.Services { @@ -10,12 +12,56 @@ namespace Bit.Core.Services public SmtpMailDeliveryService(GlobalSettings globalSettings) { + if(globalSettings.Mail?.Smtp?.Host == null) + { + throw new ArgumentNullException(nameof(globalSettings.Mail.Smtp.Host)); + } + _globalSettings = globalSettings; } - public Task SendEmailAsync(MailMessage message) + public Task SendEmailAsync(Models.Mail.MailMessage message) { - throw new NotImplementedException(); + using(var client = new SmtpClient(_globalSettings.Mail.Smtp.Host, _globalSettings.Mail.Smtp.Port)) + { + client.UseDefaultCredentials = false; + client.EnableSsl = _globalSettings.Mail.Smtp.Ssl; + client.DeliveryMethod = SmtpDeliveryMethod.Network; + 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; + foreach(var address in message.ToEmails) + { + smtpMessage.To.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 += (s, e) => + { + smtpMessage.Dispose(); + }; + + client.SendAsync(smtpMessage, null); + return Task.FromResult(0); + } } } }