Guest User

Untitled

a guest
Mar 24th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Mail;
  6. using System.Threading.Tasks;
  7. using DnsClient;
  8. using Microsoft.VisualBasic.FileIO;
  9.  
  10. namespace EmailVerification
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16.  
  17. const string sourceFile = @"c:\subscribers.csv";
  18. const string destinationFile = @"c:\invalid-subscribers.txt";
  19.  
  20. var spamEmailAddresses = new List<string>();
  21.  
  22. try
  23. {
  24. Console.WriteLine("Source File set to " + sourceFile);
  25. Console.WriteLine("Destination File set to " + destinationFile);
  26.  
  27. Console.WriteLine("Parsing the source file ...");
  28.  
  29. using (var parser = new TextFieldParser(sourceFile))
  30. {
  31. var headerRow = true;
  32. parser.Delimiters = new[] { "," };
  33. while (!parser.EndOfData)
  34. {
  35. var row = parser.ReadFields();
  36. if (row == null) continue;
  37.  
  38. if (headerRow)
  39. {
  40. headerRow = false;
  41. }
  42. else
  43. {
  44. var email = row[1];
  45.  
  46. try
  47. {
  48. var valid = IsValid(email);
  49.  
  50. if (!valid)
  51. {
  52. Console.WriteLine(email + " has been flagged");
  53.  
  54. spamEmailAddresses.Add(email);
  55. }
  56. }
  57. catch (FormatException ex)
  58. {
  59. Console.WriteLine("Exception : " + email + " : " + ex.Message);
  60.  
  61. spamEmailAddresses.Add(email);
  62. }
  63. }
  64. }
  65. }
  66.  
  67. Console.WriteLine("Writing to destination file");
  68.  
  69. using (var writer = new StreamWriter(destinationFile, false))
  70. {
  71. foreach (var spamEmailAddress in spamEmailAddresses)
  72. {
  73. writer.WriteLine(spamEmailAddress);
  74. }
  75. }
  76.  
  77. Console.WriteLine("Finished writing to destination file");
  78. }
  79. catch (Exception ex)
  80. {
  81. Console.WriteLine("Exception : " + ex.Message);
  82. }
  83.  
  84. Console.ReadKey();
  85. }
  86.  
  87. #region Sync methods
  88.  
  89. static public bool IsValid(string email)
  90. {
  91. try
  92. {
  93. var mailAddress = new MailAddress(email);
  94. var host = mailAddress.Host;
  95.  
  96. return CheckDnsEntries(host);
  97. }
  98. catch (FormatException)
  99. {
  100. return false;
  101. }
  102. }
  103.  
  104. static public bool CheckDnsEntries(string host)
  105. {
  106. try
  107. {
  108. var lookupClient = new LookupClient
  109. {
  110. Timeout = TimeSpan.FromSeconds(5)
  111. };
  112. var result = lookupClient
  113. .Query(host, QueryType.ANY);
  114. var records = result
  115. .Answers
  116. .Where(record => record.RecordType == DnsClient.Protocol.ResourceRecordType.A ||
  117. record.RecordType == DnsClient.Protocol.ResourceRecordType.AAAA ||
  118. record.RecordType == DnsClient.Protocol.ResourceRecordType.MX);
  119. return records.Any();
  120. }
  121. catch (DnsResponseException)
  122. {
  123. return false;
  124. }
  125. }
  126.  
  127. #endregion
  128.  
  129. #region Async methods
  130.  
  131. static public Task<bool> IsValidAsync(string email)
  132. {
  133. try
  134. {
  135. var mailAddress = new MailAddress(email);
  136. var host = mailAddress.Host;
  137.  
  138. return CheckDnsEntriesAsync(host);
  139. }
  140. catch (FormatException)
  141. {
  142. return Task.FromResult(false);
  143. }
  144. }
  145.  
  146. static public async Task<bool> CheckDnsEntriesAsync(string host)
  147. {
  148. try
  149. {
  150. var lookupClient = new LookupClient
  151. {
  152. Timeout = TimeSpan.FromSeconds(5)
  153. };
  154. var result = await lookupClient
  155. .QueryAsync(host, QueryType.ANY)
  156. .ConfigureAwait(false);
  157. var records = result
  158. .Answers
  159. .Where(record => record.RecordType == DnsClient.Protocol.ResourceRecordType.A ||
  160. record.RecordType == DnsClient.Protocol.ResourceRecordType.AAAA ||
  161. record.RecordType == DnsClient.Protocol.ResourceRecordType.MX);
  162. return records.Any();
  163. }
  164. catch (DnsResponseException)
  165. {
  166. return false;
  167. }
  168. }
  169.  
  170. #endregion
  171. }
  172. }
Add Comment
Please, Sign In to add comment