mirror of
https://github.com/bitwarden/server.git
synced 2025-04-05 13:08:17 -05:00
SmtpMailDeliveryService
This commit is contained in:
parent
07c5f45ae0
commit
0ee863c1d0
3
.gitignore
vendored
3
.gitignore
vendored
@ -201,4 +201,5 @@ project.lock.json
|
|||||||
*.jfm
|
*.jfm
|
||||||
mail_dist/
|
mail_dist/
|
||||||
*.refactorlog
|
*.refactorlog
|
||||||
*.scmp
|
*.scmp
|
||||||
|
src/Core/Properties/launchSettings.json
|
@ -27,8 +27,18 @@
|
|||||||
|
|
||||||
public class MailSettings
|
public class MailSettings
|
||||||
{
|
{
|
||||||
public string ApiKey { get; set; }
|
|
||||||
public string ReplyToEmail { 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
|
public class PushSettings
|
||||||
|
@ -15,8 +15,13 @@ namespace Bit.Core.Services
|
|||||||
|
|
||||||
public SendGridMailDeliveryService(GlobalSettings globalSettings)
|
public SendGridMailDeliveryService(GlobalSettings globalSettings)
|
||||||
{
|
{
|
||||||
|
if(globalSettings.Mail?.SendGridApiKey == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(globalSettings.Mail.SendGridApiKey));
|
||||||
|
}
|
||||||
|
|
||||||
_globalSettings = globalSettings;
|
_globalSettings = globalSettings;
|
||||||
_client = new SendGridClient(_globalSettings.Mail.ApiKey);
|
_client = new SendGridClient(_globalSettings.Mail.SendGridApiKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendEmailAsync(MailMessage message)
|
public async Task SendEmailAsync(MailMessage message)
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Bit.Core.Models.Mail;
|
using System.Net.Mail;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace Bit.Core.Services
|
namespace Bit.Core.Services
|
||||||
{
|
{
|
||||||
@ -10,12 +12,56 @@ namespace Bit.Core.Services
|
|||||||
|
|
||||||
public SmtpMailDeliveryService(GlobalSettings globalSettings)
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user