Upload files to 'ShipyardBusinessLogic/MailWorker'
This commit is contained in:
parent
3270235217
commit
c1411f6434
85
ShipyardBusinessLogic/MailWorker/AbstractMailWorker.cs
Normal file
85
ShipyardBusinessLogic/MailWorker/AbstractMailWorker.cs
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using ShipyardContracts.BindingModels;
|
||||||
|
using ShipyardContracts.BusinessLogicContracts;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ShipyardBusinessLogic.MailWorker
|
||||||
|
{
|
||||||
|
public abstract class AbstractMailWorker
|
||||||
|
{
|
||||||
|
protected string _mailLogin = string.Empty;
|
||||||
|
protected string _mailPassword = string.Empty;
|
||||||
|
protected string _smtpClientHost = string.Empty;
|
||||||
|
protected int _smtpClientPort;
|
||||||
|
protected string _popHost = string.Empty;
|
||||||
|
protected int _popPort;
|
||||||
|
private readonly IMessageInfoLogic _messageInfoLogic;
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
public AbstractMailWorker(ILogger<AbstractMailWorker> logger,
|
||||||
|
IMessageInfoLogic messageInfoLogic)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_messageInfoLogic = messageInfoLogic;
|
||||||
|
}
|
||||||
|
public void MailConfig(MailConfigBindingModel config)
|
||||||
|
{
|
||||||
|
_mailLogin = config.MailLogin;
|
||||||
|
_mailPassword = config.MailPassword;
|
||||||
|
_smtpClientHost = config.SmtpClientHost;
|
||||||
|
_smtpClientPort = config.SmtpClientPort;
|
||||||
|
_popHost = config.PopHost;
|
||||||
|
_popPort = config.PopPort;
|
||||||
|
_logger.LogDebug("Config: {login}, {password}, " +
|
||||||
|
"{clientHost}, { clientPOrt}, { popHost}, { popPort}",
|
||||||
|
_mailLogin, _mailPassword, _smtpClientHost, _smtpClientPort, _popHost, _popPort);
|
||||||
|
}
|
||||||
|
public async void MailSendAsync(MailSendInfoBindingModel info)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(_mailLogin) ||
|
||||||
|
string.IsNullOrEmpty(_mailPassword))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (string.IsNullOrEmpty(info.MailAddress) ||
|
||||||
|
string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress,
|
||||||
|
info.Subject);
|
||||||
|
await SendMailAsync(info);
|
||||||
|
}
|
||||||
|
public async void MailCheck()
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(_mailLogin) ||
|
||||||
|
string.IsNullOrEmpty(_mailPassword))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (string.IsNullOrEmpty(_popHost) || _popPort == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (_messageInfoLogic == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var list = await ReceiveMailAsync();
|
||||||
|
_logger.LogDebug("Check Mail: {Count} new mails", list.Count);
|
||||||
|
foreach (var mail in list)
|
||||||
|
{
|
||||||
|
_messageInfoLogic.Create(mail);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected abstract Task SendMailAsync(MailSendInfoBindingModel info);
|
||||||
|
protected abstract Task<List<MessageInfoBindingModel>> ReceiveMailAsync();
|
||||||
|
}
|
||||||
|
}
|
81
ShipyardBusinessLogic/MailWorker/MailKitWorker.cs
Normal file
81
ShipyardBusinessLogic/MailWorker/MailKitWorker.cs
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using ShipyardContracts.BindingModels;
|
||||||
|
using ShipyardContracts.BusinessLogicContracts;
|
||||||
|
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;
|
||||||
|
|
||||||
|
namespace ShipyardBusinessLogic.MailWorker
|
||||||
|
{
|
||||||
|
public class MailKitWorker : AbstractMailWorker
|
||||||
|
{
|
||||||
|
public MailKitWorker(ILogger<MailKitWorker> logger, IMessageInfoLogic messageInfoLogic) : base(logger, messageInfoLogic) { }
|
||||||
|
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;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected override async Task<List<MessageInfoBindingModel>> ReceiveMailAsync()
|
||||||
|
{
|
||||||
|
var list = new List<MessageInfoBindingModel>();
|
||||||
|
using var client = new Pop3Client();
|
||||||
|
await Task.Run(() =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client.Connect(_popHost, _popPort,
|
||||||
|
SecureSocketOptions.SslOnConnect);
|
||||||
|
client.Authenticate(_mailLogin, _mailPassword);
|
||||||
|
for (int i = 0; i < client.Count; i++)
|
||||||
|
{
|
||||||
|
var message = client.GetMessage(i);
|
||||||
|
foreach (var mail in message.From.Mailboxes)
|
||||||
|
{
|
||||||
|
list.Add(new MessageInfoBindingModel
|
||||||
|
{
|
||||||
|
DateDelivery = message.Date.DateTime,
|
||||||
|
MessageId = message.MessageId,
|
||||||
|
SenderName = mail.Address,
|
||||||
|
Subject = message.Subject,
|
||||||
|
Body = message.TextBody
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (AuthenticationException)
|
||||||
|
{ }
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
client.Disconnect(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user