Coursework_ComputerStore_Li.../ComputerStoreBusinessLogic/MailStuff/AbstractMail.cs

61 lines
2.0 KiB
C#

using ComputerStoreContracts.BindingModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreBusinessLogic.MailStuff
{
public abstract class AbstractMail
{
protected string _mailLogin = string.Empty;
protected string _mailPassword = string.Empty;
protected string _smtpClientHost = string.Empty;
protected int _smtpClientPort;
protected string _popHost = string.Empty;
protected int _popPort;
private readonly ILogger _logger;
public AbstractMail(ILogger<AbstractMail> logger)
{
_logger = logger;
}
public void MailConfig(MailConfigBindingModel config)
{
_mailLogin = config.Login;
_mailPassword = config.Password;
_smtpClientHost = config.SmtpClientHost;
_smtpClientPort = config.SmtpClientPort;
_popHost = config.PopHost;
_popPort = config.PopPort;
_logger.LogDebug("Config: {login}, {password}, {clientHost}, {clientPort}, {popHost}, {popPort}", _mailLogin, _mailPassword, _smtpClientHost, _smtpClientPort, _popHost, _popPort);
}
public async void MailSendAsync(MailMessage info)
{
if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword))
{
return;
}
if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0)
{
return;
}
if (string.IsNullOrEmpty(info.To.ToString()) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Body) || !info.Attachments.Any())
{
return;
}
_logger.LogDebug("Send E-mail: {To}, {Subject}", info.To.ToString(), info.Subject);
await SendMailAsync(info);
}
protected abstract Task SendMailAsync(MailMessage info);
}
}