mirror of
https://github.com/bitwarden/server.git
synced 2025-02-01 23:31:41 +01:00
deprecate smtpclient for mailkit smtp service
This commit is contained in:
parent
18884d564d
commit
17cc1d6543
@ -14,6 +14,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Handlebars.Net" Version="1.9.5" />
|
||||
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="2.6.0" />
|
||||
<PackageReference Include="MailKit" Version="2.1.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.DataProtection.AzureStorage" Version="2.1.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.HttpOverrides" Version="2.1.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.1.3" />
|
||||
|
@ -117,6 +117,7 @@ namespace Bit.Core
|
||||
public string Password { get; set; }
|
||||
public bool UseDefaultCredentials { get; set; } = false;
|
||||
public string AuthType { get; set; }
|
||||
public bool TrustServer { get; set; } = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using MailKit.Net.Smtp;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MimeKit;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public class MailKitSmtpMailDeliveryService : IMailDeliveryService
|
||||
{
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly ILogger<MailKitSmtpMailDeliveryService> _logger;
|
||||
private readonly string _replyDomain;
|
||||
|
||||
public MailKitSmtpMailDeliveryService(
|
||||
GlobalSettings globalSettings,
|
||||
ILogger<MailKitSmtpMailDeliveryService> 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 async Task SendEmailAsync(Models.Mail.MailMessage message)
|
||||
{
|
||||
var mimeMessage = new MimeMessage();
|
||||
mimeMessage.From.Add(new MailboxAddress(_globalSettings.SiteName, _globalSettings.Mail.ReplyToEmail));
|
||||
mimeMessage.Subject = message.Subject;
|
||||
if(!string.IsNullOrWhiteSpace(_replyDomain))
|
||||
{
|
||||
mimeMessage.MessageId = $"<{Guid.NewGuid()}@{_replyDomain}>";
|
||||
}
|
||||
|
||||
foreach(var address in message.ToEmails)
|
||||
{
|
||||
mimeMessage.To.Add(new MailboxAddress(address));
|
||||
}
|
||||
|
||||
if(message.BccEmails != null)
|
||||
{
|
||||
foreach(var address in message.BccEmails)
|
||||
{
|
||||
mimeMessage.Bcc.Add(new MailboxAddress(address));
|
||||
}
|
||||
}
|
||||
|
||||
var builder = new BodyBuilder();
|
||||
if(!string.IsNullOrWhiteSpace(message.TextContent))
|
||||
{
|
||||
builder.TextBody = message.TextContent;
|
||||
}
|
||||
builder.HtmlBody = message.HtmlContent;
|
||||
mimeMessage.Body = builder.ToMessageBody();
|
||||
|
||||
using(var client = new SmtpClient())
|
||||
{
|
||||
if(_globalSettings.Mail.Smtp.TrustServer)
|
||||
{
|
||||
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
||||
}
|
||||
|
||||
var useSsl = _globalSettings.Mail.Smtp.Port == 587 ? false : _globalSettings.Mail.Smtp.Ssl;
|
||||
await client.ConnectAsync(_globalSettings.Mail.Smtp.Host, _globalSettings.Mail.Smtp.Port, useSsl);
|
||||
|
||||
if(!_globalSettings.Mail.Smtp.UseDefaultCredentials)
|
||||
{
|
||||
await client.AuthenticateAsync(_globalSettings.Mail.Smtp.Username,
|
||||
_globalSettings.Mail.Smtp.Password);
|
||||
}
|
||||
|
||||
await client.SendAsync(mimeMessage);
|
||||
await client.DisconnectAsync(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
[Obsolete]
|
||||
public class SmtpMailDeliveryService : IMailDeliveryService
|
||||
{
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
|
@ -87,7 +87,7 @@ namespace Bit.Core.Utilities
|
||||
}
|
||||
else if(CoreHelpers.SettingHasValue(globalSettings.Mail?.Smtp?.Host))
|
||||
{
|
||||
services.AddSingleton<IMailDeliveryService, SmtpMailDeliveryService>();
|
||||
services.AddSingleton<IMailDeliveryService, MailKitSmtpMailDeliveryService>();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user