CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/MailWorker/MailKitWorker.cs

55 lines
1.9 KiB
C#
Raw Normal View History

using ElectronicsShopContracts.BusinessLogicContracts;
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 ElectronicsShopContracts.BindingModels;
using MailKit.Net.Pop3;
using MailKit.Security;
2024-07-23 20:29:10 +04:00
using MigraDoc.DocumentObjectModel;
namespace ElectronicsShopBusinessLogic.MailWorker
{
public class MailKitWorker : AbstractMailWorker
{
2024-07-24 09:53:21 +04:00
public MailKitWorker(ILogger<MailKitWorker> logger,
IClientLogic clientLogic) : base(logger, clientLogic) { }
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;
2024-07-23 20:29:10 +04:00
Attachment attachment = new Attachment(new MemoryStream(info.document), name: "Report.pdf");
if (attachment != null) {
objMailMessage.Attachments.Add(attachment);
}
objMailMessage.SubjectEncoding = Encoding.UTF8;
objMailMessage.BodyEncoding = Encoding.UTF8;
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;
}
}
}
}