Отправка pdf отчётов по почте + фикс namespace'ов файловой логики

This commit is contained in:
abazov73 2023-05-19 23:50:03 +04:00
parent f6fbc96b4f
commit d54625220a
28 changed files with 249 additions and 45 deletions

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.Abstractions" Version="7.0.0" />
<PackageReference Include="PdfSharp.MigraDoc.Standard" Version="1.51.15" />
</ItemGroup>

View File

@ -8,8 +8,8 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfectioneryBusinessLogic.OfficePackage;
using ConfectioneryBusinessLogic.OfficePackage.HelperModels;
using BankBusinessLogic.OfficePackage;
using BankBusinessLogic.OfficePackage.HelperModels;
namespace BankBusinessLogic.BusinessLogics
{

View File

@ -0,0 +1,66 @@
using BankContracts.BindingModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankBusinessLogic.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,47 @@
using BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using MailKit.Net.Pop3;
using MailKit.Security;
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;
namespace BankBusinessLogic.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;
objMailMessage.Attachments.Add(new Attachment(info.Attachment, info.FileName, "application/pdf"));
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

@ -1,12 +1,12 @@
using ConfectioneryBusinessLogic.OfficePackage.HelperEnums;
using ConfectioneryBusinessLogic.OfficePackage.HelperModels;
using BankBusinessLogic.OfficePackage.HelperEnums;
using BankBusinessLogic.OfficePackage.HelperModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage
namespace BankBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToExcel
{

View File

@ -1,12 +1,12 @@
using ConfectioneryBusinessLogic.OfficePackage.HelperEnums;
using ConfectioneryBusinessLogic.OfficePackage.HelperModels;
using BankBusinessLogic.OfficePackage.HelperEnums;
using BankBusinessLogic.OfficePackage.HelperModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage
namespace BankBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToPdf
{

View File

@ -1,12 +1,12 @@
using ConfectioneryBusinessLogic.OfficePackage.HelperEnums;
using ConfectioneryBusinessLogic.OfficePackage.HelperModels;
using BankBusinessLogic.OfficePackage.HelperEnums;
using BankBusinessLogic.OfficePackage.HelperModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage
namespace BankBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToWord
{

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.HelperEnums
namespace BankBusinessLogic.OfficePackage.HelperEnums
{
public enum ExcelStyleInfoType
{

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.HelperEnums
namespace BankBusinessLogic.OfficePackage.HelperEnums
{
public enum PdfParagraphAlignmentType
{

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.HelperEnums
namespace BankBusinessLogic.OfficePackage.HelperEnums
{
public enum WordJustificationType
{

View File

@ -1,11 +1,11 @@
using ConfectioneryBusinessLogic.OfficePackage.HelperEnums;
using BankBusinessLogic.OfficePackage.HelperEnums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.HelperModels
namespace BankBusinessLogic.OfficePackage.HelperModels
{
public class ExcelCellParameters
{

View File

@ -5,7 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.HelperModels
namespace BankBusinessLogic.OfficePackage.HelperModels
{
public class ExcelInfo
{

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.HelperModels
namespace BankBusinessLogic.OfficePackage.HelperModels
{
public class ExcelMergeParameters
{

View File

@ -5,7 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.HelperModels
namespace BankBusinessLogic.OfficePackage.HelperModels
{
public class PdfInfo
{

View File

@ -1,11 +1,11 @@
using ConfectioneryBusinessLogic.OfficePackage.HelperEnums;
using BankBusinessLogic.OfficePackage.HelperEnums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.HelperModels
namespace BankBusinessLogic.OfficePackage.HelperModels
{
public class PdfParagraph
{

View File

@ -1,11 +1,11 @@
using ConfectioneryBusinessLogic.OfficePackage.HelperEnums;
using BankBusinessLogic.OfficePackage.HelperEnums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.HelperModels
namespace BankBusinessLogic.OfficePackage.HelperModels
{
public class PdfRowParameters
{

View File

@ -5,7 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.HelperModels
namespace BankBusinessLogic.OfficePackage.HelperModels
{
public class WordInfo
{

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.HelperModels
namespace BankBusinessLogic.OfficePackage.HelperModels
{
public class WordParagraph
{

View File

@ -1,11 +1,11 @@
using ConfectioneryBusinessLogic.OfficePackage.HelperEnums;
using BankBusinessLogic.OfficePackage.HelperEnums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.HelperModels
namespace BankBusinessLogic.OfficePackage.HelperModels
{
public class WordTextProperties
{

View File

@ -1,5 +1,5 @@
using ConfectioneryBusinessLogic.OfficePackage.HelperEnums;
using ConfectioneryBusinessLogic.OfficePackage.HelperModels;
using BankBusinessLogic.OfficePackage.HelperEnums;
using BankBusinessLogic.OfficePackage.HelperModels;
using DocumentFormat.OpenXml.Office2010.Excel;
using DocumentFormat.OpenXml.Office2013.Excel;
using DocumentFormat.OpenXml.Packaging;
@ -11,7 +11,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.Implements
namespace BankBusinessLogic.OfficePackage.Implements
{
public class SaveToExcel : AbstractSaveToExcel
{

View File

@ -1,5 +1,5 @@
using ConfectioneryBusinessLogic.OfficePackage.HelperEnums;
using ConfectioneryBusinessLogic.OfficePackage.HelperModels;
using BankBusinessLogic.OfficePackage.HelperEnums;
using BankBusinessLogic.OfficePackage.HelperModels;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
@ -9,7 +9,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.Implements
namespace BankBusinessLogic.OfficePackage.Implements
{
public class SaveToPdf : AbstractSaveToPdf
{

View File

@ -1,5 +1,5 @@
using ConfectioneryBusinessLogic.OfficePackage.HelperEnums;
using ConfectioneryBusinessLogic.OfficePackage.HelperModels;
using BankBusinessLogic.OfficePackage.HelperEnums;
using BankBusinessLogic.OfficePackage.HelperModels;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
@ -9,7 +9,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryBusinessLogic.OfficePackage.Implements
namespace BankBusinessLogic.OfficePackage.Implements
{
public class SaveToWord : AbstractSaveToWord
{

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankContracts.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,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankContracts.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;
public MemoryStream Attachment { get; set; } = new MemoryStream();
public string FileName { get; set; } = string.Empty;
}
}

View File

@ -1,9 +1,11 @@
using BankContracts.BindingModels;
using BankBusinessLogic.MailWorker;
using BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using BankContracts.SearchModels;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using BankDataModels.Models;
using MailKit;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata;
using OperatorApp.Models;
@ -19,8 +21,9 @@ namespace OperatorApp.Controllers
private readonly ITransferLogic _transferLogic;
private readonly IOperatorLogic _operatorLogic;
private readonly IReportLogic _reportLogic;
private readonly AbstractMailWorker _mailWorker;
public HomeController(ILogger<HomeController> logger, IDealLogic dealLogic, IPaymentLogic paymentLogic, ITransferLogic transferLogic, IOperatorLogic operatorLogic, IReportLogic reportLogic)
public HomeController(ILogger<HomeController> logger, IDealLogic dealLogic, IPaymentLogic paymentLogic, ITransferLogic transferLogic, IOperatorLogic operatorLogic, IReportLogic reportLogic, AbstractMailWorker mailWorker)
{
_logger = logger;
_dealLogic = dealLogic;
@ -28,6 +31,7 @@ namespace OperatorApp.Controllers
_transferLogic = transferLogic;
_operatorLogic = operatorLogic;
_reportLogic = reportLogic;
_mailWorker = mailWorker;
}
public IActionResult Index()
@ -229,7 +233,8 @@ namespace OperatorApp.Controllers
{
if (APIClient.Operator == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');</script>");
Response.Redirect("/Home/Enter");
}
ViewBag.Payments = _paymentLogic.ReadList(new PaymentSearchModel { OperatorId = APIClient.Operator.Id });
return View();
@ -269,10 +274,26 @@ namespace OperatorApp.Controllers
return View(new ReportBindingModel());
}
[HttpPost]
public IActionResult TransfersReport(DateTime dateFrom, DateTime dateTo)
public void TransfersReport(DateTime dateFrom, DateTime dateTo)
{
MemoryStream report = _reportLogic.SaveTransferPurchaseToPDF(new ReportBindingModel { DateFrom = dateFrom, DateTo = dateTo });
return File(report, "application/pdf", "test.pdf");
try
{
_mailWorker.MailSendAsync(new MailSendInfoBindingModel
{
Subject = "Отчёт о закупках",
Text = "От банка \"Вы банкрот\"",
MailAddress = "",
FileName = "test.pdf",
Attachment = report
});
Response.Redirect("/");
}
catch (Exception ex)
{
Response.WriteAsync($"<script language=\"javascript\">alert('{ex.Message}');</script>");
Response.Redirect("/");
}
}
}
}

View File

@ -2,9 +2,11 @@ using BankBusinessLogic.BusinessLogics;
using BankContracts.BusinessLogicsContracts;
using BankContracts.StoragesContracts;
using BankDatabaseImplement.Implements;
using ConfectioneryBusinessLogic.OfficePackage;
using ConfectioneryBusinessLogic.OfficePackage.Implements;
using BankBusinessLogic.OfficePackage;
using BankBusinessLogic.OfficePackage.Implements;
using OperatorApp;
using BankBusinessLogic.MailWorker;
using BankContracts.BindingModels;
var builder = WebApplication.CreateBuilder(args);
@ -22,6 +24,7 @@ builder.Services.AddTransient<ICurrencyStorage, CurrencyStorage>();
builder.Services.AddTransient<AbstractSaveToWord, SaveToWord>();
builder.Services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
builder.Services.AddTransient<AbstractSaveToPdf, SaveToPdf>();
builder.Services.AddSingleton<AbstractMailWorker, MailKitWorker>();
builder.Services.AddTransient<IDealLogic, DealLogic>();
builder.Services.AddTransient<IPaymentLogic, PaymentLogic>();
@ -31,6 +34,25 @@ builder.Services.AddTransient<IReportLogic, ReportLogic>();
var app = builder.Build();
try
{
var mailSender = app.Services.GetService<AbstractMailWorker>();
mailSender?.MailConfig(new MailConfigBindingModel
{
MailLogin = builder.Configuration["MailLogin"] ?? string.Empty,
MailPassword = builder.Configuration["MailPassword"] ?? string.Empty,
SmtpClientHost = builder.Configuration["SmtpClientHost"] ?? string.Empty,
SmtpClientPort = Convert.ToInt32(builder.Configuration["SmtpClientPort"]),
PopHost = builder.Configuration["PopHost"] ?? string.Empty,
PopPort = Convert.ToInt32(builder.Configuration["PopPort"])
});
}
catch (Exception ex)
{
var logger = app.Services.GetService<ILogger>();
logger?.LogError(ex, "Îøèáêà ðàáîòû ñ ïî÷òîé");
}
APIClient.Connect(builder.Configuration);
// Configure the HTTP request pipeline.

View File

@ -1,5 +1,5 @@
@{
ViewData["Title"] = "CreatePayment";
ViewData["Title"] = "CreateTransfer";
}
<div class="text-center">
<h2 class="display-4">Создание зачисления</h2>

View File

@ -6,5 +6,11 @@
}
},
"AllowedHosts": "*",
"IPAddress": "http://localhost:5225"
"IPAddress": "http://localhost:5225",
"SmtpClientHost": "smtp.gmail.com",
"SmtpClientPort": "587",
"PopHost": "pop.gmail.com",
"PopPort": "995",
"MailLogin": "rpplab7@gmail.com",
"MailPassword": "edjc dmsf pqne gxwy"
}