48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using Contracts.BindingModels;
|
|
using Contracts.BusinessLogicsContracts;
|
|
using MailKit.Net.Pop3;
|
|
using MailKit.Security;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Net.Mail;
|
|
using System.Text;
|
|
using System.Net;
|
|
|
|
namespace BusinessLogic.MailWorker
|
|
{
|
|
public class MailKitWorker : AbstractMailWorker
|
|
{
|
|
public MailKitWorker(ILogger<MailKitWorker> logger) : base(logger) { }
|
|
protected override async Task SendMailAsync(MailSendInfoModel 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.SubjectEncoding = Encoding.UTF8;
|
|
objMailMessage.BodyEncoding = Encoding.UTF8;
|
|
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;
|
|
objSmtpClient.Credentials = new NetworkCredential(_mailLogin, _mailPassword);
|
|
await Task.Run(() => objSmtpClient.Send(objMailMessage));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|