2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2022-06-22 05:05:32 +02:00
|
|
|
using System.Linq;
|
2017-09-13 15:47:34 +09:00
|
|
|
using System.Threading.Tasks;
|
2020-06-28 17:55:27 +09:00
|
|
|
using BTCPayServer.Logging;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-12-15 21:30:46 +09:00
|
|
|
using MimeKit;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
2017-09-15 16:06:57 +09:00
|
|
|
namespace BTCPayServer.Services.Mails
|
2017-09-13 15:47:34 +09:00
|
|
|
{
|
2019-01-06 15:53:37 +01:00
|
|
|
public abstract class EmailSender : IEmailSender
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2021-11-22 17:16:08 +09:00
|
|
|
public Logs Logs { get; }
|
|
|
|
|
2020-06-28 22:07:48 -05:00
|
|
|
readonly IBackgroundJobClient _JobClient;
|
2019-01-06 15:53:37 +01:00
|
|
|
|
2021-11-22 17:16:08 +09:00
|
|
|
public EmailSender(IBackgroundJobClient jobClient, Logs logs)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2021-11-22 17:16:08 +09:00
|
|
|
Logs = logs;
|
2019-01-06 15:53:37 +01:00
|
|
|
_JobClient = jobClient ?? throw new ArgumentNullException(nameof(jobClient));
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
2019-01-06 15:53:37 +01:00
|
|
|
|
2022-06-22 05:05:32 +02:00
|
|
|
public void SendEmail(MailboxAddress email, string subject, string message)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2022-06-22 05:05:32 +02:00
|
|
|
SendEmail(new[] {email}, Array.Empty<MailboxAddress>(), Array.Empty<MailboxAddress>(), subject, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SendEmail(MailboxAddress[] email, MailboxAddress[] cc, MailboxAddress[] bcc, string subject, string message)
|
|
|
|
{
|
2019-04-05 14:58:25 +09:00
|
|
|
_JobClient.Schedule(async (cancellationToken) =>
|
2018-01-12 18:32:46 +09:00
|
|
|
{
|
2019-01-06 15:53:37 +01:00
|
|
|
var emailSettings = await GetEmailSettings();
|
|
|
|
if (emailSettings?.IsComplete() != true)
|
|
|
|
{
|
|
|
|
Logs.Configuration.LogWarning("Should have sent email, but email settings are not configured");
|
|
|
|
return;
|
|
|
|
}
|
2022-06-22 05:05:32 +02:00
|
|
|
|
2022-01-14 17:50:29 +09:00
|
|
|
using var smtp = await emailSettings.CreateSmtpClient();
|
2022-06-22 05:05:32 +02:00
|
|
|
var mail = emailSettings.CreateMailMessage(email, cc, bcc, subject, message, true);
|
2022-01-14 17:50:29 +09:00
|
|
|
await smtp.SendAsync(mail, cancellationToken);
|
|
|
|
await smtp.DisconnectAsync(true, cancellationToken);
|
2020-06-28 17:55:27 +09:00
|
|
|
}, TimeSpan.Zero);
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
2019-01-06 15:53:37 +01:00
|
|
|
|
|
|
|
public abstract Task<EmailSettings> GetEmailSettings();
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
}
|