Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net.Mail;
- using System.Threading.Tasks;
- using DnsClient;
- using Microsoft.VisualBasic.FileIO;
- namespace EmailVerification
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string sourceFile = @"c:\subscribers.csv";
- const string destinationFile = @"c:\invalid-subscribers.txt";
- var spamEmailAddresses = new List<string>();
- try
- {
- Console.WriteLine("Source File set to " + sourceFile);
- Console.WriteLine("Destination File set to " + destinationFile);
- Console.WriteLine("Parsing the source file ...");
- using (var parser = new TextFieldParser(sourceFile))
- {
- var headerRow = true;
- parser.Delimiters = new[] { "," };
- while (!parser.EndOfData)
- {
- var row = parser.ReadFields();
- if (row == null) continue;
- if (headerRow)
- {
- headerRow = false;
- }
- else
- {
- var email = row[1];
- try
- {
- var valid = IsValid(email);
- if (!valid)
- {
- Console.WriteLine(email + " has been flagged");
- spamEmailAddresses.Add(email);
- }
- }
- catch (FormatException ex)
- {
- Console.WriteLine("Exception : " + email + " : " + ex.Message);
- spamEmailAddresses.Add(email);
- }
- }
- }
- }
- Console.WriteLine("Writing to destination file");
- using (var writer = new StreamWriter(destinationFile, false))
- {
- foreach (var spamEmailAddress in spamEmailAddresses)
- {
- writer.WriteLine(spamEmailAddress);
- }
- }
- Console.WriteLine("Finished writing to destination file");
- }
- catch (Exception ex)
- {
- Console.WriteLine("Exception : " + ex.Message);
- }
- Console.ReadKey();
- }
- #region Sync methods
- static public bool IsValid(string email)
- {
- try
- {
- var mailAddress = new MailAddress(email);
- var host = mailAddress.Host;
- return CheckDnsEntries(host);
- }
- catch (FormatException)
- {
- return false;
- }
- }
- static public bool CheckDnsEntries(string host)
- {
- try
- {
- var lookupClient = new LookupClient
- {
- Timeout = TimeSpan.FromSeconds(5)
- };
- var result = lookupClient
- .Query(host, QueryType.ANY);
- var records = result
- .Answers
- .Where(record => record.RecordType == DnsClient.Protocol.ResourceRecordType.A ||
- record.RecordType == DnsClient.Protocol.ResourceRecordType.AAAA ||
- record.RecordType == DnsClient.Protocol.ResourceRecordType.MX);
- return records.Any();
- }
- catch (DnsResponseException)
- {
- return false;
- }
- }
- #endregion
- #region Async methods
- static public Task<bool> IsValidAsync(string email)
- {
- try
- {
- var mailAddress = new MailAddress(email);
- var host = mailAddress.Host;
- return CheckDnsEntriesAsync(host);
- }
- catch (FormatException)
- {
- return Task.FromResult(false);
- }
- }
- static public async Task<bool> CheckDnsEntriesAsync(string host)
- {
- try
- {
- var lookupClient = new LookupClient
- {
- Timeout = TimeSpan.FromSeconds(5)
- };
- var result = await lookupClient
- .QueryAsync(host, QueryType.ANY)
- .ConfigureAwait(false);
- var records = result
- .Answers
- .Where(record => record.RecordType == DnsClient.Protocol.ResourceRecordType.A ||
- record.RecordType == DnsClient.Protocol.ResourceRecordType.AAAA ||
- record.RecordType == DnsClient.Protocol.ResourceRecordType.MX);
- return records.Any();
- }
- catch (DnsResponseException)
- {
- return false;
- }
- }
- #endregion
- }
- }
Add Comment
Please, Sign In to add comment