2018-01-12 18:32:46 +09:00
|
|
|
|
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;
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
IBackgroundJobClient _JobClient;
|
2019-01-06 15:53:37 +01:00
|
|
|
|
|
|
|
|
|
public EmailSender(IBackgroundJobClient jobClient)
|
2017-10-27 17:53:04 +09:00
|
|
|
|
{
|
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
|
|
|
|
|
|
|
|
|
public void SendEmail(string email, string subject, string message)
|
2017-10-27 17:53:04 +09:00
|
|
|
|
{
|
2019-01-06 15:53:37 +01:00
|
|
|
|
_JobClient.Schedule(async () =>
|
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;
|
|
|
|
|
}
|
|
|
|
|
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);
|
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
|
|
|
|
}
|