btcpayserver/BTCPayServer/Services/Mails/EmailSettings.cs

68 lines
2.2 KiB
C#
Raw Normal View History

2017-09-27 14:18:09 +09:00
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
2021-12-31 16:59:02 +09:00
using System.Threading;
using System.Threading.Tasks;
2022-03-11 10:17:50 +01:00
using BTCPayServer.Client.Models;
2021-12-31 16:59:02 +09:00
using MailKit.Net.Smtp;
using MimeKit;
using Newtonsoft.Json;
2017-09-27 14:18:09 +09:00
namespace BTCPayServer.Services.Mails
{
2022-03-11 10:17:50 +01:00
public class EmailSettings :EmailSettingsData
2017-09-27 14:18:09 +09:00
{
public bool IsComplete()
{
return !string.IsNullOrWhiteSpace(Server) &&
Port is int &&
!string.IsNullOrWhiteSpace(Login) &&
!string.IsNullOrWhiteSpace(Password);
}
2017-09-27 14:18:09 +09:00
public MimeMessage CreateMailMessage(MailboxAddress to, string subject, string message, bool isHtml)
2018-05-05 01:42:42 +09:00
{
var bodyBuilder = new BodyBuilder();
if (isHtml)
2018-05-05 01:42:42 +09:00
{
bodyBuilder.HtmlBody = message;
2018-05-05 01:42:42 +09:00
}
else
{
bodyBuilder.TextBody = message;
}
return new MimeMessage(
2021-12-31 16:59:02 +09:00
from: new[] { new MailboxAddress(From, !string.IsNullOrWhiteSpace(FromDisplay) ? From : FromDisplay) },
to: new[] { to },
subject,
bodyBuilder.ToMessageBody());
}
public async Task<SmtpClient> CreateSmtpClient()
{
SmtpClient client = new SmtpClient();
using var connectCancel = new CancellationTokenSource(10000);
try
{
if (Extensions.IsLocalNetwork(Server))
{
client.CheckCertificateRevocation = false;
#pragma warning disable CA5359 // Do Not Disable Certificate Validation
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
#pragma warning restore CA5359 // Do Not Disable Certificate Validation
}
await client.ConnectAsync(Server, Port.Value, MailKit.Security.SecureSocketOptions.Auto, connectCancel.Token);
await client.AuthenticateAsync(Login, Password, connectCancel.Token);
}
catch
{
client.Dispose();
throw;
}
return client;
}
}
2017-09-27 14:18:09 +09:00
}