using DnsClient; using MailServer; using SMTPServer; using SMTPServer.Exceptions; using System; using System.Collections.Generic; namespace MailServer { public class MailQueue { private static List Mails = null; public static async void AddToMesssageQueueAsync(Mail mail) { #pragma warning disable IDE0017 // Die Objektinitialisierung kann vereinfacht werden. var qmail = new QueueMail(mail); #pragma warning restore IDE0017 // Die Objektinitialisierung kann vereinfacht werden. qmail.DestinationDnsNames = await GetDNSEntries.GetMXRecordAsync(mail.GetSenderDomain()); qmail.DestinationIps = await GetDNSEntries.GetIPAddressFromDnsArrayAsync(qmail.DestinationDnsNames); lock (Mails) { if (Mails == null) { Mails = new List(); } 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; } } } }