Gismatullin.ISEbd-21.STO.Co.../ServiceStation/ServiceSourceBusinessLogic/MailKitWorker/MailKitWorker.cs

47 lines
1.8 KiB
C#

using Microsoft.Extensions.Logging;
using ServiceStationContracts.BindingModels;
using ServiceStationContracts.BusinessLogicContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ServiceSourceBusinessLogic.MailKitWorker {
public class MailKitWorker :AbstractMailWorker {
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;
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;
}
}
}
}