1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-21 12:05:42 +01:00

SmtpMailDeliveryService

This commit is contained in:
Kyle Spearrin 2017-05-30 22:40:02 -04:00
parent 07c5f45ae0
commit 0ee863c1d0
4 changed files with 68 additions and 6 deletions

3
.gitignore vendored
View File

@ -201,4 +201,5 @@ project.lock.json
*.jfm
mail_dist/
*.refactorlog
*.scmp
*.scmp
src/Core/Properties/launchSettings.json

View File

@ -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

View File

@ -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)

View File

@ -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);
}
}
}
}