Рабочая почта. УРАА
This commit is contained in:
parent
2cf4884362
commit
a5ebf1c7ad
@ -0,0 +1,70 @@
|
||||
using SchoolAgainStudyContracts.BindingModel;
|
||||
using SchoolAgainStudyContracts.BusinessLogicContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SchoolAgainStudyBusinessLogic.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.Path))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject);
|
||||
await SendMailAsync(info);
|
||||
}
|
||||
|
||||
protected abstract Task SendMailAsync(MailSendInfoBindingModel info);
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
using SchoolAgainStudyContracts.BindingModel;
|
||||
using SchoolAgainStudyContracts.BusinessLogicContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MailKit.Net.Pop3;
|
||||
using MailKit.Security;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using System.Text;
|
||||
using SchoolAgainStudyContracts.StorageContracts;
|
||||
using SchoolAgainStudyContracts.SearchModel;
|
||||
|
||||
namespace SchoolAgainStudyBusinessLogic.MailWorker
|
||||
{
|
||||
public class MailKitWorker : AbstractMailWorker
|
||||
{
|
||||
|
||||
public MailKitWorker(ILogger<MailKitWorker> logger, IStudentStorage clientStorage) : 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SchoolAgainStudyContracts.BindingModel
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SchoolAgainStudyContracts.BindingModel
|
||||
{
|
||||
public class MailSendInfoBindingModel
|
||||
{
|
||||
public string MailAddress { get; set; } = string.Empty;
|
||||
|
||||
public string Subject { get; set; } = string.Empty;
|
||||
|
||||
public string Path { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
@ -11,7 +11,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="BindingModel\" />
|
||||
<Folder Include="BusinessLogicContracts\" />
|
||||
<Folder Include="SearchModel\" />
|
||||
<Folder Include="StorageContracts\" />
|
||||
|
11
SchoolAgainStudy/StudentWebClient/App.config
Normal file
11
SchoolAgainStudy/StudentWebClient/App.config
Normal 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="labrpp89@gmail.com" />
|
||||
<add key="MailPassword" value="ajtl tzre ozva bbtz" />
|
||||
</appSettings>
|
||||
</configuration>
|
@ -1,5 +1,7 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using DocumentFormat.OpenXml.Wordprocessing;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using SchoolAgainStudyBusinessLogic.MailWorker;
|
||||
using SchoolAgainStudyContracts.BindingModel;
|
||||
using SchoolAgainStudyContracts.BusinessLogicContracts;
|
||||
using SchoolAgainStudyContracts.SearchModel;
|
||||
@ -21,8 +23,9 @@ namespace StudentWebClient.Controllers
|
||||
private readonly IInterestLogic _interest;
|
||||
private readonly IStudentLogic _student;
|
||||
private readonly IReportLogic _report;
|
||||
private readonly AbstractMailWorker mailSender;
|
||||
public HomeController(ILogger<HomeController> logger, IDiyLogic diy,
|
||||
IProductLogic product , ITaskLogic task , IInterestLogic interest, IStudentLogic student, IReportLogic report)
|
||||
IProductLogic product , ITaskLogic task , IInterestLogic interest, IStudentLogic student, IReportLogic report, AbstractMailWorker abstractMailWorker)
|
||||
{
|
||||
_logger = logger;
|
||||
_diy = diy;
|
||||
@ -31,6 +34,25 @@ namespace StudentWebClient.Controllers
|
||||
_interest = interest;
|
||||
_student = student;
|
||||
_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, "Ошибка работы с почтой");
|
||||
}
|
||||
}
|
||||
|
||||
public IActionResult Index()
|
||||
@ -142,6 +164,7 @@ namespace StudentWebClient.Controllers
|
||||
[HttpGet]
|
||||
public IActionResult CreateInterest()
|
||||
{
|
||||
|
||||
return View();
|
||||
}
|
||||
[HttpPost]
|
||||
@ -461,11 +484,18 @@ namespace StudentWebClient.Controllers
|
||||
{
|
||||
throw new Exception("Не верные даты");
|
||||
}
|
||||
string path = $"C:\\Users\\Вова\\Documents\\Учеба\\Reports\\{APIClient.Student.Name} от {_dateFrom.ToString("dd/MM/yyyy")}.pdf";
|
||||
_report.SaveInterestsToPdfFile
|
||||
(new ReportBindingModel { FileName = $"C:\\Users\\Вова\\Documents\\Учеба\\Reports\\{APIClient.Student.Name} от {_dateFrom.ToString("dd/MM/yyyy")}.pdf" ,
|
||||
(new ReportBindingModel { FileName = path,
|
||||
DateFrom = _dateFrom, DateTo = _dateTo, StudentId = APIClient.Student.Id});
|
||||
|
||||
var items = _report.GetInterests(new ReportBindingModel { DateFrom = _dateFrom, DateTo = _dateTo, StudentId = APIClient.Student.Id });
|
||||
mailSender.MailSendAsync(new MailSendInfoBindingModel
|
||||
{
|
||||
MailAddress = APIClient.Student.Email,
|
||||
Subject = "Заказ номер",
|
||||
Path = path
|
||||
});
|
||||
return PartialView("_LessonListPartial", items);
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,14 @@ using SchoolAgainStudyContracts.BusinessLogicContracts;
|
||||
using SchoolAgainStudyContracts.StorageContracts;
|
||||
using SchoolAgainStudyDataBaseImplements.Implements;
|
||||
using StudentWebClient;
|
||||
using SchoolAgainStudyBusinessLogic.MailWorker;
|
||||
using SchoolAgainStudyContracts.BindingModel;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Logging.SetMinimumLevel(LogLevel.Trace);
|
||||
builder.Logging.AddLog4Net("log4net.config");
|
||||
|
||||
builder.Services.AddTransient<IStudentStorage, StudentStorage>();
|
||||
builder.Services.AddTransient<IInterestStorage, InterestStorage>();
|
||||
builder.Services.AddTransient<IDiyStorage, DiyStorage>();
|
||||
@ -29,12 +33,15 @@ builder.Services.AddTransient<ITeacherLogic, TeacherLogic>();
|
||||
builder.Services.AddTransient<IMaterialLogic, MaterialLogic>();
|
||||
builder.Services.AddTransient<ILessonLogic, LessonLogic>();
|
||||
|
||||
builder.Services.AddSingleton<AbstractMailWorker, MailKitWorker>();
|
||||
|
||||
builder.Services.AddTransient<AbstractSaveToExcelStudent, SaveToExcelStudent>();
|
||||
builder.Services.AddTransient<AbstractSaveToWordStudent, SaveToWordStudent>();
|
||||
builder.Services.AddTransient<AbstractSaveToPdfStudent, SaveToPdfStudent>();
|
||||
builder.Services.AddTransient<AbstractSaveToExcelTeacher, SaveToExcelTeacher>();
|
||||
builder.Services.AddTransient<AbstractSaveToWordTeacher, SaveToWordTeacher>();
|
||||
builder.Services.AddTransient<AbstractSaveToPdfTeacher, SaveToPdfTeacher>();
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllersWithViews();
|
||||
var app = builder.Build();
|
||||
|
@ -6,6 +6,16 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Remove="App.config" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="App.config">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
Loading…
Reference in New Issue
Block a user