mail worker

This commit is contained in:
dasha 2023-05-18 18:04:23 +04:00
parent acdd3e1554
commit 610c76a402
15 changed files with 262 additions and 8 deletions

View File

@ -1,4 +1,5 @@
using HardwareShopBusinessLogic.OfficePackage;
using HardwareShopBusinessLogic.MailWorker;
using HardwareShopBusinessLogic.OfficePackage;
using HardwareShopBusinessLogic.OfficePackage.HelperModels;
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.BusinessLogicsContracts;
@ -13,16 +14,22 @@ namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper
private readonly IGoodStorage _goodStorage;
private readonly IUserStorage _userStorage;
private readonly AbstractSaveToExcel _saveToExcel;
private readonly AbstractSaveToWord _saveToWord;
public ReportStorekeeperLogic(IComponentStorage componentStorage, AbstractSaveToExcel abstractSaveToExcel, AbstractSaveToWord abstractSaveToWord, IGoodStorage goodStorage)
private readonly AbstractMailWorker _mailWorker;
public ReportStorekeeperLogic(IComponentStorage componentStorage, AbstractSaveToExcel abstractSaveToExcel, AbstractSaveToWord abstractSaveToWord, IGoodStorage goodStorage, AbstractMailWorker abstractMailWorker, IUserStorage userStorage)
{
_componentStorage = componentStorage;
_saveToExcel = abstractSaveToExcel;
_saveToWord = abstractSaveToWord;
_goodStorage = goodStorage;
_mailWorker = abstractMailWorker;
_userStorage = userStorage;
}
public List<ReportBuildGoodViewModel> GetBuildGood(List<GoodViewModel> goods)
{
@ -109,5 +116,18 @@ namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper
File.Delete(model.FileName);
return file;
}
public bool SendReportOnMail(int userId, string subject, string text)
{
var user = _userStorage.GetElement(new() { Id = userId });
if (user == null) return false;
_mailWorker.MailSendAsync(new()
{
MailAddress = user.Email,
Subject = subject,
Text = text
});
return true;
}
}
}

View File

@ -94,19 +94,19 @@ namespace HardwareShopBusinessLogic.BusinessLogics
{
return;
}
if (string.IsNullOrEmpty(model.Login) || model.Login.Length > 20)
if (string.IsNullOrEmpty(model.Login) || model.Login.Length > 40)
{
throw new ArgumentNullException("Нет логина пользователя или длина превышает 20 символов", nameof(model.Login));
throw new ArgumentNullException("Нет логина пользователя или длина превышает 40 символов", nameof(model.Login));
}
if (string.IsNullOrEmpty(model.Email) || model.Email.Length > 30)
if (string.IsNullOrEmpty(model.Email) || model.Email.Length > 40)
{
throw new ArgumentNullException("Нет почты пользователя или длина превышает 30 символов", nameof(model.Email));
throw new ArgumentNullException("Нет почты пользователя или длина превышает 40 символов", nameof(model.Email));
}
if (!Regex.IsMatch(model.Email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$", RegexOptions.IgnoreCase))
{
throw new ArgumentException("Неправильно введенная почта", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.Password) || model.Password.Length > 30 || model.Password.Contains(' '))
if (string.IsNullOrEmpty(model.Password) || model.Password.Length > 40 || model.Password.Contains(' '))
{
throw new ArgumentNullException("Нет пароля пользователя или пароль содержит пробелы", nameof(model.Password));
}

View File

@ -8,12 +8,14 @@
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.20.0" />
<PackageReference Include="MailKit" Version="4.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HardwareShopContracts\HardwareShopContracts.csproj" />
<ProjectReference Include="..\HardwareShopDatabaseImplement\HardwareShopDatabaseImplement.csproj" />
<ProjectReference Include="..\HardwareShopDataModels\HardwareShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,62 @@
using HardwareShopContracts.BindingModels;
using Microsoft.Extensions.Logging;
namespace HardwareShopBusinessLogic.MailWorker
{
public abstract class AbstractMailWorker
{
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 AbstractMailWorker(ILogger<AbstractMailWorker> logger)
{
_logger = logger;
}
public void MailConfig(MailConfigBindingModel config)
{
_mailLogin = config.MailLogin;
_mailPassword = config.MailPassword;
_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(MailSendInfoBindingModel info)
{
if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword))
{
return;
}
if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0)
{
return;
}
if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text))
{
return;
}
_logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject);
await SendMailAsync(info);
}
protected abstract Task SendMailAsync(MailSendInfoBindingModel info);
}
}

View File

@ -0,0 +1,42 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.BusinessLogicsContracts;
using MailKit.Net.Pop3;
using MailKit.Security;
using Microsoft.Extensions.Logging;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace HardwareShopBusinessLogic.MailWorker
{
public class MailKitWorker : AbstractMailWorker
{
public MailKitWorker(ILogger<MailKitWorker> logger) : base(logger) { }
protected override async Task SendMailAsync(MailSendInfoBindingModel info)
{
using var objMailMessage = new MailMessage();
using var objSmtpClient = new SmtpClient(_smtpClientHost, _smtpClientPort);
try
{
objMailMessage.From = new MailAddress(_mailLogin);
objMailMessage.To.Add(new MailAddress(info.MailAddress));
objMailMessage.Subject = info.Subject;
objMailMessage.Body = info.Text;
objMailMessage.SubjectEncoding = Encoding.UTF8;
objMailMessage.BodyEncoding = Encoding.UTF8;
objSmtpClient.UseDefaultCredentials = false;
objSmtpClient.EnableSsl = true;
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
objSmtpClient.Credentials = new NetworkCredential(_mailLogin, _mailPassword);
await Task.Run(() => objSmtpClient.Send(objMailMessage));
}
catch (Exception)
{
throw;
}
}
}
}

View File

@ -431,5 +431,16 @@ namespace HardwareShopStorekeeperApp.Controllers
<ReportBindingModel, List<ReportComponentsViewModel>>("api/report/componentsreport", reportModel);
return list!;
}
[HttpPost]
public void ReportSendOnMail([FromBody] ReportBindingModel reportModel)
{
if (APIClient.User == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
reportModel.UserId = APIClient.User.Id;
APIClient.PostRequest("api/report/componentsreportsendonmail", reportModel);
}
}
}

View File

@ -57,7 +57,18 @@
alert(xhr.responseText);
})
} else { alert("empty fields") }
})
onmail.addEventListener("click" () =>{
console.log('try to send email')
$.ajax({
method: "POST",
contentType: "application/json",
url: `/Storekeeper/ReportSendOnMail`,
data: JSON.stringify(reportModel)
}).fail(function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
})
})
function reloadTable() {
resultString = '';

View File

@ -0,0 +1,17 @@
namespace HardwareShopContracts.BindingModels
{
public class MailConfigBindingModel
{
public string MailLogin { get; set; } = string.Empty;
public string MailPassword { get; set; } = string.Empty;
// откуда отправляем письма
public string SmtpClientHost { get; set; } = string.Empty;
public int SmtpClientPort { get; set; }
// откуда получаем
public string PopHost { get; set; } = string.Empty;
public int PopPort { get; set; }
}
}

View File

@ -0,0 +1,11 @@
namespace HardwareShopContracts.BindingModels
{
public class MailSendInfoBindingModel
{
public string MailAddress { get; set; } = string.Empty;
public string Subject { get; set; } = string.Empty;
public string Text { get; set; } = string.Empty;
}
}

View File

@ -0,0 +1,19 @@
using HardwareShopDataModels.Models;
namespace HardwareShopContracts.BindingModels
{
public class MessageInfoBindingModel : IMessageInfoModel
{
public string MessageId { get; set; } = string.Empty;
public int? UserId { get; set; }
public string SenderName { get; set; } = string.Empty;
public string Subject { get; set; } = string.Empty;
public string Body { get; set; } = string.Empty;
public DateTime DateDelivery { get; set; }
}
}

View File

@ -31,5 +31,11 @@ namespace HardwareShopContracts.BusinessLogicsContracts
/// </summary>
/// <param name="model"></param>
byte[] SaveBuildGoodToExcelFile(ReportBindingModel model, List<GoodViewModel> goods);
/// <summary>
/// Отправление отчета на почту
/// </summary>
/// <param name="model"></param>
bool SendReportOnMail(int userId, string subject, string text);
}
}

View File

@ -0,0 +1,17 @@
namespace HardwareShopDataModels.Models
{
public interface IMessageInfoModel
{
string MessageId { get; }
int? UserId { get; }
string SenderName { get; }
DateTime DateDelivery { get; }
string Subject { get; }
string Body { get; }
}
}

View File

@ -60,5 +60,19 @@ namespace HardwareShopRestApi.Controllers
throw;
}
}
[HttpPost]
public void ComponentsReportSendOnMail(ReportBindingModel model)
{
try
{
_reportStorekeeperLogic.SendReportOnMail(model.UserId, "заголовок", "текст");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения сведений по полученным пользователем комплектующим за период");
throw;
}
}
}
}

View File

@ -1,7 +1,9 @@
using HardwareShopBusinessLogic.BusinessLogics;
using HardwareShopBusinessLogic.BusinessLogics.Storekeeper;
using HardwareShopBusinessLogic.MailWorker;
using HardwareShopBusinessLogic.OfficePackage;
using HardwareShopBusinessLogic.OfficePackage.Implements;
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.BuisnessLogicsContracts;
using HardwareShopContracts.BusinessLogicsContracts;
using HardwareShopContracts.StoragesContracts;
@ -36,6 +38,8 @@ builder.Services.AddTransient<IWorkerReportLogic, WorkerReportLogic>();
builder.Services.AddTransient<AbstractSaveToWord, SaveToWord>();
builder.Services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
builder.Services.AddTransient<AbstractMailWorker, MailKitWorker>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
@ -46,6 +50,17 @@ builder.Services.AddSwaggerGen(c =>
var app = builder.Build();
var mailSender = app.Services.GetService<AbstractMailWorker>();
mailSender?.MailConfig(new MailConfigBindingModel
{
MailLogin = builder.Configuration?.GetSection("MailLogin")?.Value?.ToString() ?? string.Empty,
MailPassword = builder.Configuration?.GetSection("MailPassword")?.Value?.ToString() ?? string.Empty,
SmtpClientHost = builder.Configuration?.GetSection("SmtpClientHost")?.Value?.ToString() ?? string.Empty,
SmtpClientPort = Convert.ToInt32(builder.Configuration?.GetSection("SmtpClientPort")?.Value?.ToString()),
PopHost = builder.Configuration?.GetSection("PopHost")?.Value?.ToString() ?? string.Empty,
PopPort = Convert.ToInt32(builder.Configuration?.GetSection("PopPort")?.Value?.ToString())
});
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{

View File

@ -5,5 +5,12 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"SmtpClientHost": "smtp.gmail.com",
"SmtpClientPort": "587",
"PopHost": "pop.gmail.com",
"PopPort": "995",
"MailLogin": "orderbuyerzxc@gmail.com",
"MailPassword": "sjxa uvgn pepe tatl"
}