Beispiel #1
0
        /// <summary>
        /// Return a list of MX records
        /// </summary>
        /// <param name="emailList">List of eamils to check MX records from</param>
        /// <returns></returns>
        static public Dictionary <string, List <string> > GetMxRecords(ref List <Email> emailList)
        {
            var OutputList          = new Dictionary <string, List <string> >();
            var ExcludedDomainsList = new List <IgnoredEmails>();

            var list = CsvParser.LoadDomain("Domains - Customers");

            if (list != null)
            {
                ExcludedDomainsList.AddRange(list);
            }
            list = CsvParser.LoadDomain("Domains - Common");
            if (list != null)
            {
                ExcludedDomainsList.AddRange(list);
            }
            list = CsvParser.LoadDomain("Domains - AV Blacklist");
            if (list != null)
            {
                ExcludedDomainsList.AddRange(list);
            }
            list = CsvParser.LoadDomain("Domains - Supplier list");
            if (list != null)
            {
                ExcludedDomainsList.AddRange(list);
            }

            var domainList = new List <string>();

            foreach (var email in emailList)
            {
                domainList.Add(ReturnDomain(email.EmailAddress));
            }

            domainList = domainList.Distinct().ToList();

            //Remove domains that are in the excluded list
            var tempDomainList     = domainList;
            var removedDomainsList = new List <string>();

            if (ExcludedDomainsList != null)
            {
                foreach (var excludedDomain in ExcludedDomainsList)
                {
                    foreach (var domain in domainList.ToList().Where(domain => excludedDomain.Name == domain))
                    {
                        tempDomainList.Remove(domain);
                        removedDomainsList.Add(domain);
                    }
                }
            }

            Console.WriteLine($"Removed {domainList.Count - tempDomainList.Count} domains");
            domainList = tempDomainList;

            var tempEmailList = emailList;

            foreach (var email in emailList.ToList())
            {
                foreach (var removedDomain in removedDomainsList)
                {
                    if (ReturnDomain(email.EmailAddress) == removedDomain)
                    {
                        tempEmailList.Remove(email);
                    }
                }
            }

            Console.WriteLine($"Removed {emailList.Count - tempEmailList.Count} emails");
            emailList = tempEmailList;

            foreach (var record in domainList)
            {
                try
                {
                    //string rec = ReturnDomain(record);
                    Func <List <string> > getMxRecords = new Func <List <string> >(() => GetMxRecord(record));
                    OutputList.Add(record, RunTaskWithTimeout(getMxRecords, 10) ?? new List <string>()
                    {
                        "No records"
                    });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return(OutputList);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            //CheckDns();
            try
            {
                Data = new JsonParser().Load;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            Dictionary <string, List <string> > mxRecords = new Dictionary <string, List <string> >();

            //Add existing output to output list to save old records
            List <EmailOutput> checkedEmails = new List <EmailOutput>();
            var list = CsvParser.LoadEmailOutput("Checked Emails");

            if (list != null)
            {
                checkedEmails = list;
            }

            Console.WriteLine($"\nLoaded {checkedEmails.Count} existing records");

            //Load all emails that need to be checked
            List <Email> emailsToCheck = new List <Email>();
            var          tempEmailList = CsvParser.LoadEmail("Email list");

            if (tempEmailList == null)
            {
                Console.WriteLine("Cannot find 'Email list.csv'\nPlease add it to the CSV folder");
                Console.ReadKey();
            }
            else
            {
                emailsToCheck.AddRange(tempEmailList);
            }

            var tempList = new List <Email>();

            foreach (var email in emailsToCheck)
            {
                if (email.EmailAddress != "" && email.DomainName == "" && email.FirstName == "" && email.LastName == "")
                {
                    tempList.Add(new Email(email.EmailAddress));
                }
                else
                {
                    tempList.Add(email);
                }
            }

            emailsToCheck = tempList;


            List <IgnoredEmails> ignorePrefix = new List <IgnoredEmails>();

            try
            {
                ignorePrefix.AddRange(CsvParser.LoadDomain("Prefix - Common.csv"));

                if (ignorePrefix.Count > 0)
                {
                    tempList = emailsToCheck;
                    foreach (var email in emailsToCheck.ToList())
                    {
                        foreach (var prefix in ignorePrefix)
                        {
                            if (returnName(email.EmailAddress) == prefix.Name)
                            {
                                tempList.Remove(email);
                            }
                        }
                    }

                    emailsToCheck = tempList;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            //Remove emails that already have been checked
            try
            {
                var tempCheckList = checkedEmails;
                foreach (var existingEmail in tempCheckList)
                {
                    foreach (var email in emailsToCheck)
                    {
                        if (Compare(existingEmail, email))
                        {
                            Console.WriteLine($"Removed {email.EmailAddress} : x{checkedEmails.RemoveAll(x => x.EmailAddress == email.EmailAddress)}");
                        }
                    }
                }
                checkedEmails.Distinct().ToList();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            mxRecords = GetMxRecords(ref emailsToCheck);

            foreach (var record in mxRecords)
            {
                Console.WriteLine(record.Key);
                foreach (var mxRecord in record.Value)
                {
                    if (mxRecord.Contains("no record"))
                    {
                        mxRecords[record.Key].Remove(mxRecord);
                    }
                    else
                    {
                        Console.WriteLine(mxRecord);
                    }
                }
            }
            var newlyAddedCounter = 0;

            mxRecords = getTcpInfo(mxRecords);
            //Check emails against domain to see if they match, then run telnet
            //Console.WriteLine($"\nChecking 0/{emailsToCheck.Count}");
            foreach (var email in emailsToCheck)
            {
                Console.WriteLine($"Checking {emailsToCheck.IndexOf(email) + 1}/{emailsToCheck.Count}\nEmail: {email.EmailAddress}");

                try
                {
                    foreach (var tcpRecord in mxRecords[email.DomainName])
                    {
                        Func <bool> telnetCheck = new Func <bool>(() => IsEmailAccountValid(new TCPClient(tcpRecord, email.EmailAddress)));
                        if (RunTaskWithTimeout(telnetCheck, 10))
                        {
                            checkedEmails.Add(new EmailOutput(email));
                            newlyAddedCounter++;
                            Thread.Sleep(20);
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Skipped domain {email.DomainName}\nNot in domain list");
                    Debug.WriteLine(ex.Message);
                }
            }

            Console.WriteLine($"\nDomains for emails found {checkedEmails.Count}/{emailsToCheck.Count}\nMissing: {emailsToCheck.Count - newlyAddedCounter}");

            checkedEmails = checkedEmails.Distinct().ToList();

            checkedEmails.SaveToCsv("Live emails");

            Console.WriteLine($"\nSaved {checkedEmails.Count} records");
            Console.WriteLine("Program ended\nRead key");

            Data = new Data(mxRecords, checkedEmails, emailsToCheck, ignorePrefix);
            new JsonParser().Save(Data);
            Console.ReadKey(true);
        }