implement mail sending

This commit is contained in:
DavidMakarov 2024-05-29 19:29:32 +04:00
parent 5710c1a22b
commit d3d94e39a8
8 changed files with 157 additions and 10 deletions

View File

@ -18,8 +18,4 @@
<ProjectReference Include="..\FactoryContracts\FactoryContracts.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="MailWorker\" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,56 @@
using FactoryContracts.BindingModels;
using Microsoft.Extensions.Logging;
namespace FactoryBuisinessLogic.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,43 @@
using FactoryContracts.BindingModels;
using Microsoft.Extensions.Logging;
using System.Net.Mail;
using System.Net.Mime;
using System.Net;
using System.Text;
namespace FactoryBuisinessLogic.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;
Attachment attachment = new Attachment("D:\\temp\\report.pdf", new ContentType(MediaTypeNames.Application.Pdf));
objMailMessage.Attachments.Add(attachment);
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

@ -0,0 +1,12 @@
namespace FactoryContracts.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,9 @@
namespace FactoryContracts.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

@ -1,4 +1,5 @@
using FactoryContracts.BindingModels;
using FactoryBuisinessLogic.MailWorker;
using FactoryContracts.BindingModels;
using FactoryContracts.BusinessLogicsContracts;
using FactoryContracts.SearchModels;
using FactoryContracts.ViewModels;
@ -19,13 +20,14 @@ namespace FactoryStorekeeperApp.Controllers
private readonly IProductLogic productLogic;
private readonly IPlanProductionLogic planProductionLogic;
private readonly IStorekeeperReportLogic storekeeperReportLogic;
private readonly AbstractMailWorker abstractMailWorker;
private static bool IsLoggedIn { get { return Client.client != null; } }
public HomeController(ILogger<HomeController> logger, IClientLogic clientLogic,
IRequirementLogic requirementLogic, IProductLogic productLogic,
IMachineLogic machineLogic, IPlanProductionLogic planProductionLogic,
IStorekeeperReportLogic storekeeperReportLogic)
IStorekeeperReportLogic storekeeperReportLogic, AbstractMailWorker abstractMailWorker)
{
_logger = logger;
this.clientLogic = clientLogic;
@ -34,6 +36,7 @@ namespace FactoryStorekeeperApp.Controllers
this.machineLogic = machineLogic;
this.planProductionLogic = planProductionLogic;
this.storekeeperReportLogic = storekeeperReportLogic;
this.abstractMailWorker = abstractMailWorker;
}
public IActionResult Index()
@ -538,8 +541,13 @@ namespace FactoryStorekeeperApp.Controllers
DateTo = dateto,
FileName="D:\\temp\\report.pdf"
});
// TODO: implement sending
throw new NotImplementedException();
}
abstractMailWorker.MailSendAsync(new MailSendInfoBindingModel
{
MailAddress = Client.client.Email,
Subject = $"Отчет по станкам пользователя {Client.client.Login}",
Text = $"Отчет по станкам с {datefrom.ToShortDateString()} по {dateto.ToShortDateString()}"
});
return Redirect("~/Home/Index");
}
}
}

View File

@ -1,6 +1,8 @@
using FactoryBuisinessLogic.MailWorker;
using FactoryBusinessLogic.BusinessLogics;
using FactoryBusinessLogic.OfficePackage;
using FactoryBusinessLogic.OfficePackage.Implements;
using FactoryContracts.BindingModels;
using FactoryContracts.BusinessLogicsContracts;
using FactoryContracts.StoragesContracts;
using FactoryDatabaseImplement.Implements;
@ -25,10 +27,24 @@ builder.Services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
builder.Services.AddTransient<AbstractSaveToPdf, SaveToPdf>();
builder.Services.AddTransient<AbstractSaveToWord, SaveToWord>();
builder.Services.AddSingleton<AbstractMailWorker, MailKitWorker>();
builder.Services.AddControllersWithViews();
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

@ -7,5 +7,12 @@
},
"AllowedHosts": "*",
"IPAddress": "http://localhost:5283/"
"IPAddress": "http://localhost:5283/",
"SmtpClientHost": "smtp.gmail.com",
"SmtpClientPort": "587",
"PopHost": "pop.gmail.com",
"PopPort": "995",
"MailLogin": "labworker83@gmail.com",
"MailPassword": "wpxc drvx lhqb uqpe"
}