dotnet-core_mail-server/MailServer/MailQueue.cs

91 lines
2.5 KiB
C#
Raw Permalink Normal View History

2016-11-26 11:11:27 +00:00
using DnsClient;
2016-11-27 17:38:47 +00:00
using MailServer;
using SMTPServer;
2016-11-26 11:11:27 +00:00
using SMTPServer.Exceptions;
using System;
using System.Collections.Generic;
2016-11-27 17:38:47 +00:00
namespace MailServer
2016-11-26 11:11:27 +00:00
{
public class MailQueue
{
private static List<QueueMail> Mails = null;
public static async void AddToMesssageQueueAsync(Mail mail)
{
2016-11-27 17:38:47 +00:00
#pragma warning disable IDE0017 // Die Objektinitialisierung kann vereinfacht werden.
2016-11-26 11:11:27 +00:00
var qmail = new QueueMail(mail);
2016-11-27 17:38:47 +00:00
#pragma warning restore IDE0017 // Die Objektinitialisierung kann vereinfacht werden.
2016-11-26 11:11:27 +00:00
qmail.DestinationDnsNames = await GetDNSEntries.GetMXRecordAsync(mail.GetSenderDomain());
qmail.DestinationIps = await GetDNSEntries.GetIPAddressFromDnsArrayAsync(qmail.DestinationDnsNames);
lock (Mails)
{
if (Mails == null)
{
Mails = new List<QueueMail>();
}
Mails.Add(qmail);
}
}
public static QueueMail GetNextMail()
{
lock (Mails)
{
if(Mails.Count < 1)
{
throw new NoMailsInQueueException();
}
var m = Mails[0];
Mails.Remove(m);
m.Count++;
Mails.Add(m);
return m;
}
}
public static void RemoveMailFromQueue(QueueMail mail)
{
lock (Mails)
{
Mails.Remove(mail);
}
}
public class QueueMail
{
public DateTime QueueEntered { get; private set; }
public int Count { get; set; }
public Mail Mail { get; private set; }
public string[] DestinationIps { get; set; }
public string[] DestinationDnsNames { get; set; }
public int DestinationIndex { get; set; }
public QueueMail (Mail mail)
{
Mail = mail;
QueueEntered = DateTime.Now;
Count = 0;
}
public byte[] GetIpBytes() => GetIpBytes(DestinationIndex);
public byte[] GetIpBytes(int index)
{
var parts = DestinationIps[index].Split('.');
var bytes = new byte[parts.Length];
for(int i = 0; i < parts.Length; i++)
{
bytes[i] = byte.Parse(parts[i]);
}
return bytes;
}
}
}
}