btcpayserver/BTCPayServer/Services/Mails/EmailSender.cs

41 lines
1.2 KiB
C#
Raw Normal View History

using BTCPayServer.Logging;
using Microsoft.Extensions.Logging;
2017-09-27 14:18:09 +09:00
using System;
using System.Net.Mail;
2017-09-13 15:47:34 +09:00
using System.Threading.Tasks;
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
{
IBackgroundJobClient _JobClient;
2019-01-06 15:53:37 +01:00
public EmailSender(IBackgroundJobClient jobClient)
{
2019-01-06 15:53:37 +01:00
_JobClient = jobClient ?? throw new ArgumentNullException(nameof(jobClient));
}
2019-01-06 15:53:37 +01:00
public void SendEmail(string email, string subject, string message)
{
2019-01-06 15:53:37 +01:00
_JobClient.Schedule(async () =>
{
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;
}
var smtp = emailSettings.CreateSmtpClient();
var mail = new MailMessage(emailSettings.From, email, subject, message)
{
IsBodyHtml = true
};
await smtp.SendMailAsync(mail);
2017-09-27 14:18:09 +09:00
2019-01-06 15:53:37 +01:00
}, TimeSpan.Zero);
}
2019-01-06 15:53:37 +01:00
public abstract Task<EmailSettings> GetEmailSettings();
}
2017-09-13 15:47:34 +09:00
}