107 lines
3.8 KiB
C#
107 lines
3.8 KiB
C#
|
using BankContracts.BindingModels.Messages;
|
|||
|
using BankDataModels.Enums;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Mail;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace BankBusinessLogic.MailWorker
|
|||
|
{
|
|||
|
// Класс, отвечающий за отправку письма на почту
|
|||
|
public class MailKitWorker
|
|||
|
{
|
|||
|
private string _mailLogin = string.Empty;
|
|||
|
|
|||
|
private string _mailPassword = string.Empty;
|
|||
|
|
|||
|
private string _smtpClientHost = string.Empty;
|
|||
|
|
|||
|
private int _smtpClientPort;
|
|||
|
|
|||
|
private readonly ILogger logger;
|
|||
|
|
|||
|
// Конструктор
|
|||
|
public MailKitWorker(ILogger<MailKitWorker> logger)
|
|||
|
{
|
|||
|
this.logger = logger;
|
|||
|
}
|
|||
|
|
|||
|
public void MailConfig(MailConfigBindingModel model)
|
|||
|
{
|
|||
|
_mailLogin = model.MailLogin;
|
|||
|
_mailPassword = model.MailPassword;
|
|||
|
_smtpClientHost= model.SmtpClientHost;
|
|||
|
_smtpClientPort = model.SmtpClientPort;
|
|||
|
}
|
|||
|
|
|||
|
public async void 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;
|
|||
|
|
|||
|
MemoryStream memory = new(info.File);
|
|||
|
|
|||
|
if (info.Role == MailsEnum.Клиент)
|
|||
|
{
|
|||
|
if (info.TypeDoc == TypeDocEnum.PDF)
|
|||
|
{
|
|||
|
objMailMessage.Attachments.Add(new Attachment(memory, "Отчёт_для_клиента.pdf", "application/pdf"));
|
|||
|
}
|
|||
|
|
|||
|
if (info.TypeDoc == TypeDocEnum.EXCEL)
|
|||
|
{
|
|||
|
objMailMessage.Attachments.Add(new Attachment(memory, "Отчёт_для_клиента.xlsx", "application/xlsx"));
|
|||
|
}
|
|||
|
|
|||
|
if (info.TypeDoc == TypeDocEnum.WORD)
|
|||
|
{
|
|||
|
objMailMessage.Attachments.Add(new Attachment(memory, "Отчёт_для_клиента.docx", "application/docx"));
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (info.TypeDoc == TypeDocEnum.PDF)
|
|||
|
{
|
|||
|
objMailMessage.Attachments.Add(new Attachment(memory, "Отчёт_для_кассира.pdf", "application/pdf"));
|
|||
|
}
|
|||
|
|
|||
|
if (info.TypeDoc == TypeDocEnum.EXCEL)
|
|||
|
{
|
|||
|
objMailMessage.Attachments.Add(new Attachment(memory, "Отчёт_для_кассира.xlsx", "application/xlsx"));
|
|||
|
}
|
|||
|
|
|||
|
if (info.TypeDoc == TypeDocEnum.WORD)
|
|||
|
{
|
|||
|
objMailMessage.Attachments.Add(new Attachment(memory, "Отчёт_для_кассира.docx", "application/docx"));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|