Почта работает.

This commit is contained in:
Никита Волков 2024-12-24 17:10:55 +04:00
parent 2b867a6fa3
commit 9581aaa847
9 changed files with 61 additions and 31 deletions

View File

@ -12,13 +12,11 @@ namespace BeautyStudioBusinessLogic.MailWorker
protected int _smtpClientPort;
protected string _popHost = string.Empty;
protected int _popPort;
private readonly IStoreKeeperLogic _executorLogic;
private readonly ILogger _logger;
public AbstractMailWorker(ILogger<AbstractMailWorker> logger, IStoreKeeperLogic executorLogic)
public AbstractMailWorker(ILogger<AbstractMailWorker> logger)
{
_logger = logger;
_executorLogic = executorLogic;
}
public void MailConfig(MailConfigBindingModel config)
@ -44,12 +42,18 @@ namespace BeautyStudioBusinessLogic.MailWorker
return;
}
if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text))
if (string.IsNullOrEmpty(info.MailAddress))
{
return;
}
if (string.IsNullOrEmpty(info.Subject)) {
return;
_logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject);
}
//if (string.IsNullOrEmpty(info.Text)) {
// return;
//}
_logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject);
await SendMailAsync(info);
}

View File

@ -11,7 +11,7 @@ namespace BeautyStudioBusinessLogic.MailWorker
{
public class MailKitWorker : AbstractMailWorker
{
public MailKitWorker(ILogger<MailKitWorker> logger, IStoreKeeperLogic _storekeeperLogic) : base(logger, _storekeeperLogic) { }
public MailKitWorker(ILogger<MailKitWorker> logger) : base(logger) { }
protected override async Task SendMailAsync(MailSendInfoBindingModel info)
{
@ -23,12 +23,17 @@ namespace BeautyStudioBusinessLogic.MailWorker
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("C:\\Reports\\pdffile.pdf", new ContentType(MediaTypeNames.Application.Pdf));
objMailMessage.Attachments.Add(attachment);
if (info.Text != null)
{
objMailMessage.Body = info.Text;
}
if (info.Pdf != null)
{
var attachment = new Attachment(new MemoryStream(info.Pdf), info.FileName);
objMailMessage.Attachments.Add(attachment);
}
objSmtpClient.UseDefaultCredentials = false;
objSmtpClient.EnableSsl = true;
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

View File

@ -24,7 +24,8 @@ namespace BeautyStudioBusinessLogic.OfficePackage
CreateParagraph(new PdfParagraph
{
Text = info.Title,
Style = "NormalTitle"
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
// Период выборки данных
@ -32,20 +33,13 @@ namespace BeautyStudioBusinessLogic.OfficePackage
CreateParagraph(new PdfParagraph
{
Text = $"С {info.DateFrom.ToShortDateString()} по {info.DateTo.ToShortDateString()}",
Style = "Normal"
Style = "Normal",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
// Создаем таблицу с двумя колонками
CreateTable(new List<string> { "5cm", "10cm" });
// Создаем заголовок таблицы
CreateRow(new PdfRowParameters
{
Texts = new List<string> { "Услуга", "Процедуры", "Косметика" },
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
// Записываем основную информацию
foreach (var report in info.Services)
{

View File

@ -9,6 +9,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BeautyStudioBusinessLogic.OfficePackage;
using System.Runtime.CompilerServices;
namespace BeautyStudioBusinessLogic.OfficePackage.Implements
{

View File

@ -27,11 +27,31 @@ namespace BeautyStudioDatabaseImplement.Implements
return new();
}
using var context = new BeautyStudioDatabase();
if (model.DateCreate.HasValue)
return context.Services.Where(x => x.StoreKeeperId == model.StoreKeeperId).Where(x => x.DateCreate <= model.DateComplete && x.DateCreate >= model.DateCreate).Select(x => x.GetViewModel).ToList();
else
return context.Services.Where(x => x.StoreKeeperId == model.StoreKeeperId).Select(x => x.GetViewModel).ToList();
}
DateTime? dateCreateUtc = model.DateCreate.HasValue
? DateTime.SpecifyKind(model.DateCreate.Value, DateTimeKind.Utc)
: null;
DateTime? dateCompleteUtc = model.DateComplete.HasValue
? DateTime.SpecifyKind(model.DateComplete.Value, DateTimeKind.Utc)
: null;
if (model.DateCreate.HasValue)
{
return context.Services
.Where(x => x.StoreKeeperId == model.StoreKeeperId)
.Where(x => x.DateCreate <= dateCompleteUtc && x.DateCreate >= dateCreateUtc)
.Select(x => x.GetViewModel)
.ToList();
}
else
{
return context.Services
.Where(x => x.StoreKeeperId == model.StoreKeeperId)
.Select(x => x.GetViewModel)
.ToList();
}
}
public ServiceViewModel? GetElement(ServiceSearchModel model)
{
using var context = new BeautyStudioDatabase();

View File

@ -18,6 +18,7 @@
<ProjectReference Include="..\BeautyStudioBusinessLogic\BeautyStudioBusinessLogic.csproj" />
<ProjectReference Include="..\BeautyStudioContracts\BeautyStudioContracts.csproj" />
<ProjectReference Include="..\BeautyStudioDatabaseImplement\BeautyStudioDatabaseImplement.csproj" />
<ProjectReference Include="..\StoreKeeperWebApp\StoreKeeperWebApp.csproj" />
</ItemGroup>
</Project>

View File

@ -7,6 +7,7 @@ using BeautyStudioBusinessLogic.OfficePackage;
using BeautyStudioBusinessLogic.OfficePackage.Implements;
using System.Reflection.PortableExecutable;
using BeautyStudioBusinessLogic.MailWorker;
using StoreKeeperWebApp;
var builder = WebApplication.CreateBuilder(args);
@ -32,8 +33,6 @@ builder.Services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
builder.Services.AddTransient<AbstractSaveToWord, SaveToWord>();
builder.Services.AddTransient<AbstractSaveToPdf, SaveToPdf>();
builder.Services.AddTransient<AbstractMailWorker, MailKitWorker>();
builder.Services.AddControllers();
@ -49,7 +48,12 @@ mailSender?.MailConfig(new MailConfigBindingModel
PopPort = Convert.ToInt32(builder.Configuration?.GetSection("PopPort")?.Value?.ToString())
});
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseAuthorization();
app.MapControllers();

View File

@ -286,8 +286,8 @@ namespace StoreKeeperWebApp
DateTo = endDate!.Value,
FileName = stream,
Services = reports,
Title = "Отчет"
});
Title = "Отчет",
}); ;
byte[] report = stream.GetBuffer();
_mail.MailSendAsync(new() { MailAddress = UserStoreKeeper.user!.StoreKeeperEmail, Subject = "Отчет", FileName = "PdfReport.pdf", Pdf = report });
}

View File

@ -11,5 +11,6 @@
"PopHost": "pop.gmail.com",
"PopPort": "995",
"MailLogin": "rpplabs724@gmail.com",
"MailPassword": "tlta buqp gljg jiso"
"MailPassword": "idsj gftu cgnr fflz",
"EnableSSl": "true"
}