CourseWork_BankYouBankrupt/BankYouBankrupt/BankYouBankruptBusinessLogic/MailWorker/MailKitWorker.cs

82 lines
2.7 KiB
C#
Raw Normal View History

2023-05-18 13:39:10 +04:00
using BankYouBankruptContracts.BusinessLogicsContracts;
2023-05-18 14:45:28 +04:00
using BankYouBankruptContracts.BindingModels;
2023-05-18 13:39:10 +04:00
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;
using MailKit.Net.Pop3;
using MailKit.Security;
2023-05-20 01:43:20 +04:00
using BankYouBankruptDataModels.Enums;
2023-05-18 13:39:10 +04:00
namespace BankYouBankruptBusinessLogic.MailWorker
{
2023-05-20 01:43:20 +04:00
//класс, отвечающий за отправку письма
public class MailKitWorker
2023-05-18 13:39:10 +04:00
{
2023-05-20 01:43:20 +04:00
private string _mailLogin = string.Empty;
2023-05-18 13:39:10 +04:00
2023-05-20 01:43:20 +04:00
private string _mailPassword = string.Empty;
2023-05-18 13:39:10 +04:00
2023-05-20 01:43:20 +04:00
private string _smtpClientHost = string.Empty;
2023-05-18 13:39:10 +04:00
2023-05-20 01:43:20 +04:00
private int _smtpClientPort;
2023-05-18 13:39:10 +04:00
2023-05-20 01:43:20 +04:00
private readonly ILogger logger;
2023-05-18 13:39:10 +04:00
2023-05-20 01:43:20 +04:00
public MailKitWorker(ILogger<MailKitWorker> logger)
{
this.logger = logger;
}
2023-05-18 13:39:10 +04:00
2023-05-20 01:43:20 +04:00
public void MailConfig(MailConfigBindingModel config)
{
_mailLogin = config.MailLogin;
_mailPassword = config.MailPassword;
_smtpClientHost = config.SmtpClientHost;
_smtpClientPort = config.SmtpClientPort;
}
2023-05-18 13:39:10 +04:00
2023-05-20 01:43:20 +04:00
public async void SendMailAsync(MailSendInfoBindingModel info)
{
using var objMailMessage = new MailMessage();
using var objSmtpClient = new SmtpClient(_smtpClientHost, _smtpClientPort);
2023-05-18 13:39:10 +04:00
2023-05-20 01:43:20 +04:00
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;
2023-05-18 13:39:10 +04:00
2023-05-20 01:43:20 +04:00
MemoryStream ms = new(info.File);
2023-05-18 13:39:10 +04:00
2023-05-20 01:43:20 +04:00
if(info.Role == MailsEnum.Клиент)
{
objMailMessage.Attachments.Add(new Attachment(ms, "Отчёт_поартам.pdf", "application/pdf"));
}
else
{
objMailMessage.Attachments.Add(new Attachment(ms, "Отчёт_по_счетам.pdf", "application/pdf"));
}
2023-05-18 13:39:10 +04:00
2023-05-20 01:43:20 +04:00
objSmtpClient.UseDefaultCredentials = false;
objSmtpClient.EnableSsl = true;
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
objSmtpClient.Credentials = new NetworkCredential(_mailLogin, _mailPassword);
2023-05-18 13:39:10 +04:00
2023-05-20 01:43:20 +04:00
await Task.Run(() => objSmtpClient.Send(objMailMessage));
}
catch (Exception)
{
throw;
}
}
2023-05-18 13:39:10 +04:00
}
}