работа с почтой, исправления пространств имен

This commit is contained in:
Мк Игорь 2023-05-26 02:11:24 +04:00
parent f9a6359762
commit 63512cf103
29 changed files with 153 additions and 43 deletions

View File

@ -1,6 +1,6 @@
using BlacksmithWorkshopBusinessLogic.OfficePackage;
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels;
using BlacksmithWorkshopBusinessLogic.OfficePackage.Implements;
using CarServiceBusinessLogic.OfficePackage;
using CarServiceBusinessLogic.OfficePackage.HelperModels;
using CarServiceBusinessLogic.OfficePackage.Implements;
using CarServiceContracts.BindingModels;
using CarServiceContracts.BusinessLogicsContracts;
using CarServiceContracts.StorageContracts;

View File

@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.20.0" />
<PackageReference Include="MailKit" Version="4.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="PdfSharp.MigraDoc.Standard" Version="1.51.15" />
</ItemGroup>

View File

@ -0,0 +1,42 @@
using CarServiceContracts.BindingModels;
using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.Extensions.Logging;
namespace CarServiceBusinessLogic.MailWorker
{
public abstract class AbstractMailWorker
{
protected string _mailLogin = string.Empty;
protected string _mailPassword = string.Empty;
protected string _smtpClientHost = string.Empty;
protected int _smtpClientPort;
private readonly ILogger _logger;
public AbstractMailWorker(ILogger<AbstractMailWorker> logger)
{
_logger = logger;
_mailLogin = "igors20111@gmail.com";
_mailPassword = "xzjt gzal aoek rjmp";
_smtpClientHost = "smtp.gmail.com";
_smtpClientPort = 465;
_logger.LogDebug("Config: {login}, {password}, {clientHost}, {clientPort}", _mailLogin, _mailPassword, _smtpClientHost, _smtpClientPort);
}
public void MailSend(MailSendInfoBindingModel info)
{
if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword))
{
return;
}
if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0)
{
return;
}
if (string.IsNullOrEmpty(info.MailAddress))
{
return;
}
_logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject);
SendMail(info);
}
protected abstract void SendMail(MailSendInfoBindingModel info);
}
}

View File

@ -0,0 +1,34 @@
using Microsoft.Extensions.Logging;
using System.Net;
using MimeKit;
using CarServiceContracts.BindingModels;
namespace CarServiceBusinessLogic.MailWorker
{
public class MailKitWorker : AbstractMailWorker
{
public MailKitWorker(ILogger<MailKitWorker> logger) : base(logger) { }
protected override void SendMail(MailSendInfoBindingModel info)
{
using var message = new MimeMessage();
using var smtpClient = new MailKit.Net.Smtp.SmtpClient();
smtpClient.Connect(_smtpClientHost, 465, true);
smtpClient.Authenticate(new NetworkCredential(_mailLogin, _mailPassword));
try
{
var builder = new BodyBuilder();
builder.Attachments.Add("C:\\Users\\igors\\source\\repos\\ISEbd-21_Melnikov_I.O._CarService\\CarService\\CarServiceWebApp\\Files\\ReportPdf.pdf");
message.Body = builder.ToMessageBody();
message.From.Add (new MailboxAddress("СТО Руки-крюки", _mailLogin));
message.To.Add(new MailboxAddress("igors_2011", "igors-2011@mail.ru"));
message.Subject = info.Subject;
smtpClient.Send(message);
}
catch (Exception)
{
throw;
}
}
}
}

View File

@ -1,7 +1,7 @@
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums;
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels;
using CarServiceBusinessLogic.OfficePackage.HelperEnums;
using CarServiceBusinessLogic.OfficePackage.HelperModels;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage
namespace CarServiceBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToExcel
{

View File

@ -1,7 +1,7 @@
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums;
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels;
using CarServiceBusinessLogic.OfficePackage.HelperEnums;
using CarServiceBusinessLogic.OfficePackage.HelperModels;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage
namespace CarServiceBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToPdf
{

View File

@ -1,7 +1,7 @@
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums;
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels;
using CarServiceBusinessLogic.OfficePackage.HelperEnums;
using CarServiceBusinessLogic.OfficePackage.HelperModels;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage
namespace CarServiceBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToWord
{

View File

@ -1,4 +1,4 @@
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums
namespace CarServiceBusinessLogic.OfficePackage.HelperEnums
{
public enum ExcelStyleInfoType
{

View File

@ -1,4 +1,4 @@
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums
namespace CarServiceBusinessLogic.OfficePackage.HelperEnums
{
public enum PdfParagraphAlignmentType
{

View File

@ -1,4 +1,4 @@
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums
namespace CarServiceBusinessLogic.OfficePackage.HelperEnums
{
public enum WordJustificationType
{

View File

@ -1,5 +1,5 @@
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels
using CarServiceBusinessLogic.OfficePackage.HelperEnums;
namespace CarServiceBusinessLogic.OfficePackage.HelperModels
{
public class ExcelCellParameters
{

View File

@ -1,7 +1,7 @@

using CarServiceContracts.ViewModels;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels
namespace CarServiceBusinessLogic.OfficePackage.HelperModels
{
public class ExcelInfo
{

View File

@ -1,4 +1,4 @@
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels
namespace CarServiceBusinessLogic.OfficePackage.HelperModels
{
public class ExcelMergeParameters
{

View File

@ -1,6 +1,6 @@
using CarServiceContracts.ViewModels;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels
namespace CarServiceBusinessLogic.OfficePackage.HelperModels
{
public class PdfInfo
{

View File

@ -1,6 +1,6 @@
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums;
using CarServiceBusinessLogic.OfficePackage.HelperEnums;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels
namespace CarServiceBusinessLogic.OfficePackage.HelperModels
{
public class PdfParagraph
{

View File

@ -1,6 +1,6 @@
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums;
using CarServiceBusinessLogic.OfficePackage.HelperEnums;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels
namespace CarServiceBusinessLogic.OfficePackage.HelperModels
{
public class PdfRowParameters
{

View File

@ -1,6 +1,6 @@
using CarServiceContracts.ViewModels;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels
namespace CarServiceBusinessLogic.OfficePackage.HelperModels
{
public class WordInfo
{

View File

@ -1,4 +1,4 @@
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels
namespace CarServiceBusinessLogic.OfficePackage.HelperModels
{
public class WordParagraph
{

View File

@ -1,6 +1,6 @@
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums;
using CarServiceBusinessLogic.OfficePackage.HelperEnums;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels
namespace CarServiceBusinessLogic.OfficePackage.HelperModels
{
public class WordTextProperties
{

View File

@ -1,5 +1,5 @@
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums;
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels;
using CarServiceBusinessLogic.OfficePackage.HelperEnums;
using CarServiceBusinessLogic.OfficePackage.HelperModels;
using DocumentFormat.OpenXml.Office2010.Excel;
using DocumentFormat.OpenXml.Office2013.Excel;
using DocumentFormat.OpenXml.Office2016.Excel;
@ -7,7 +7,7 @@ using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.Implements
namespace CarServiceBusinessLogic.OfficePackage.Implements
{
public class SaveToExcel : AbstractSaveToExcel
{

View File

@ -1,10 +1,10 @@
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums;
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels;
using CarServiceBusinessLogic.OfficePackage.HelperEnums;
using CarServiceBusinessLogic.OfficePackage.HelperModels;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.Implements
namespace CarServiceBusinessLogic.OfficePackage.Implements
{
public class SaveToPdf : AbstractSaveToPdf
{

View File

@ -1,10 +1,10 @@
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums;
using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels;
using CarServiceBusinessLogic.OfficePackage.HelperEnums;
using CarServiceBusinessLogic.OfficePackage.HelperModels;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
namespace BlacksmithWorkshopBusinessLogic.OfficePackage.Implements
namespace CarServiceBusinessLogic.OfficePackage.Implements
{
public class SaveToWord : AbstractSaveToWord
{

View File

@ -0,0 +1,9 @@
namespace CarServiceContracts.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 CarServiceContracts.BindingModels;
using CarServiceBusinessLogic.MailWorker;
using CarServiceContracts.BindingModels;
using CarServiceContracts.BusinessLogicsContracts;
using CarServiceWebApp.Models;
using log4net;
@ -10,16 +11,19 @@ namespace CarServiceWebApp.Controllers
{
private readonly IReportLogic _reportLogic;
private readonly IWorkLogic _workLogic;
private readonly AbstractMailWorker _mailWorker;
private static List<int> SelectedWorks = new();
private static ReportBindingModel PaymentsModel = new()
{
FileName = "C:\\Users\\igors\\source\\repos\\ISEbd-21_Melnikov_I.O._CarService\\CarService\\CarServiceWebApp\\Files\\ReportPdf.pdf"
};
public ReportController(IReportLogic reportLogic, IWorkLogic workLogic)
public ReportController(IReportLogic reportLogic, IWorkLogic workLogic, AbstractMailWorker mailWorker)
{
_reportLogic = reportLogic;
_workLogic = workLogic;
_mailWorker = mailWorker;
}
public IActionResult Index()
@ -121,5 +125,13 @@ namespace CarServiceWebApp.Controllers
return File(fileBytes, "application/force-download", fileName);
}
public IActionResult Mail() => View();
[HttpPost]
public IActionResult Mail(string MailAddress)
{
_reportLogic.SavePaymentsToPdfFile(PaymentsModel);
_mailWorker.MailSend(new() { MailAddress = MailAddress, Subject = $"Отчет по оплатам с {(PaymentsModel.DateFrom ?? new DateTime()).ToShortDateString()} по {(PaymentsModel.DateTo ?? new DateTime()).ToShortDateString()}" });
return Redirect("~/Report/Index");
}
}
}

View File

@ -1,6 +1,7 @@
using BlacksmithWorkshopBusinessLogic.OfficePackage;
using BlacksmithWorkshopBusinessLogic.OfficePackage.Implements;
using CarServiceBusinessLogic.BusinessLogics;
using CarServiceBusinessLogic.MailWorker;
using CarServiceBusinessLogic.OfficePackage;
using CarServiceBusinessLogic.OfficePackage.Implements;
using CarServiceContracts.BusinessLogicsContracts;
using CarServiceContracts.StorageContracts;
using CarServiceDatabase.Implements;
@ -31,6 +32,8 @@ builder.Services.AddTransient<AbstractSaveToWord, SaveToWord>();
builder.Services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
builder.Services.AddTransient<AbstractSaveToPdf, SaveToPdf>();
builder.Services.AddSingleton<AbstractMailWorker, MailKitWorker>();
var app = builder.Build();
// Configure the HTTP request pipeline.

View File

@ -0,0 +1,6 @@
@using CarServiceContracts.BindingModels
@model MailSendInfoBindingModel
<form method="post">
<center><input name="MailAddress" type="text" value="affh" /></center>
<div><center><input type="submit" class="btn btn-primary" value="Отправить на почту" /></center></div>
</form>

View File

@ -57,5 +57,8 @@
{
<p>Нет оплат за указанный период</p>
}
<div><center><a asp-controller="Report" asp-action="SaveToPdf" class="btn btn-primary">Сохранить в пдф</a></center></div>
</div>
<div><center><a asp-controller="Report" asp-action="Mail" class="btn btn-primary">Отправить на почту</a></center></div>
</div>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="c:/BlacksmithWorkshopRestApi.log"/>
<file value="c:/CarServiceRestApi.log"/>
<appendToFile value="true"/>
<maximumFileSize value="100KB"/>
<maxSizeRollBackups value="2"/>