This commit is contained in:
artiogf 2023-05-25 01:26:13 +04:00
parent c0f13a287a
commit 9b91763678
8 changed files with 202 additions and 3 deletions

View File

@ -0,0 +1,69 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TourCompanyContracts.BindingModels;
namespace TourCompanyBusinessLogic.MailKit
{
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.Path))
{
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,47 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using TourCompanyContracts.BindingModels;
using TourCompanyContracts.StoragesContracts;
namespace TourCompanyBusinessLogic.MailKit
{
public class MailKitWorker : AbstractMailWorker
{
public MailKitWorker(ILogger<MailKitWorker> logger, IUserStorage userStorage) : 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.Attachments.Add(new Attachment(info.Path));
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

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="SmtpClientHost" value="smtp.gmail.com" />
<add key="SmtpClientPort" value="587" />
<add key="PopHost" value="pop.gmail.com" />
<add key="PopPort" value="995" />
<add key="MailLogin" value="asdqr647@gmail.com" />
<add key="MailPassword" value="nzxwolohajhfqmoe" />
</appSettings>
</configuration>

View File

@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.IdentityModel.Tokens;
using System.Diagnostics;
using TourCompanyBusinessLogic.MailKit;
using TourCompanyClientApp.Models;
using TourCompanyContracts.BindingModels;
using TourCompanyContracts.BusinessLogicsContracts;
@ -11,6 +12,8 @@ using TourCompanyContracts.ViewModels;
using TourCompanyDatabaseImplement.Models;
using TourCompanyDataModels.Enums;
using TourCompanyDataModels.Models;
using Newtonsoft.Json;
namespace TourCompanyClientApp.Controllers
{
@ -29,8 +32,9 @@ namespace TourCompanyClientApp.Controllers
private readonly ITripLogic _trip;
private readonly IReportLogic _report;
private readonly AbstractMailWorker mailSender;
public HomeController(ILogger<HomeController> logger , IExecurtionLogic execurtion, ITourLogic tour, IUserLogic user, ITourGroupLogic tourGroup, IPlaceVisitLogic placeVisitLogic, IGidLogic gid, ITripLogic trip, IReportLogic report)
public HomeController(ILogger<HomeController> logger , IExecurtionLogic execurtion, ITourLogic tour, IUserLogic user, ITourGroupLogic tourGroup, IPlaceVisitLogic placeVisitLogic, IGidLogic gid, ITripLogic trip, AbstractMailWorker abstractMailWorker, IReportLogic report)
{
_logger = logger;
_execurtion = execurtion;
@ -41,6 +45,25 @@ namespace TourCompanyClientApp.Controllers
_gid = gid;
_trip = trip;
_report = report;
try
{
mailSender = abstractMailWorker;
mailSender?.MailConfig(new MailConfigBindingModel
{
MailLogin = System.Configuration.ConfigurationManager.AppSettings["MailLogin"] ?? string.Empty,
MailPassword = System.Configuration.ConfigurationManager.AppSettings["MailPassword"] ?? string.Empty,
SmtpClientHost = System.Configuration.ConfigurationManager.AppSettings["SmtpClientHost"] ?? string.Empty,
SmtpClientPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpClientPort"]),
PopHost = System.Configuration.ConfigurationManager.AppSettings["PopHost"] ?? string.Empty,
PopPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PopPort"])
});
}
catch (Exception ex)
{
logger?.LogError(ex, "Ошибка работы с почтой");
}
}
[HttpGet]
@ -573,6 +596,12 @@ namespace TourCompanyClientApp.Controllers
});
var items = _report.GetTours(new ReportBindingModel { DateFrom = _dateFrom, DateTo = _dateTo, UserId = APIClient.User.Id });
mailSender.MailSendAsync(new MailSendInfoBindingModel
{
MailAddress = APIClient.User.Email,
Subject = $"Отчет от {dateFrom} по {dateTo}",
Path = path
});
return PartialView("TourPartial", items);
}
}

View File

@ -1,4 +1,5 @@
using TourCompanyBusinessLogic.BusinessLogics;
using TourCompanyBusinessLogic.MailKit;
using TourCompanyBusinessLogic.OfficePackage;
using TourCompanyBusinessLogic.OfficePackage.Implements;
using TourCompanyClientApp;
@ -30,6 +31,8 @@ builder.Services.AddTransient<AbstractSaveToExcelUser, SaveToExcelUser>();
builder.Services.AddTransient<AbstractSaveToWordUser, SaveToWordUser>();
builder.Services.AddTransient<AbstractSaveToPdfUser, SaveToPdfUser>();
builder.Services.AddSingleton<AbstractMailWorker, MailKitWorker>();
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();

View File

@ -5,9 +5,9 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
</ItemGroup>
<ItemGroup>

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TourCompanyContracts.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,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TourCompanyContracts.BindingModels
{
public class MailSendInfoBindingModel
{
public string MailAddress { get; set; } = string.Empty;
public string Subject { get; set; } = string.Empty;
public string Path { get; set; } = string.Empty;
}
}