Guest User

Untitled

a guest
Feb 19th, 2018
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.38 KB | None | 0 0
  1. public class EmailTest {
  2. private static int hear(BufferedReader in) throws IOException {
  3. String line = null;
  4. int res = 0;
  5.  
  6. while ((line = in.readLine()) != null) {
  7. String pfx = line.substring(0, 3);
  8. try {
  9. res = Integer.parseInt(pfx);
  10. } catch (Exception ex) {
  11. res = -1;
  12. }
  13. if (line.charAt(3) != '-')
  14. break;
  15. }
  16.  
  17. return res;
  18. }
  19.  
  20. private static void say(BufferedWriter wr, String text) throws IOException {
  21. wr.write(text + "rn");
  22. wr.flush();
  23.  
  24. return;
  25. }
  26.  
  27. @SuppressWarnings({ "rawtypes", "unchecked" })
  28. private static ArrayList getMX(String hostName) throws NamingException {
  29. // Perform a DNS lookup for MX records in the domain
  30. Hashtable env = new Hashtable();
  31. env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
  32. DirContext ictx = new InitialDirContext(env);
  33. Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
  34. Attribute attr = attrs.get("MX");
  35.  
  36. // if we don't have an MX record, try the machine itself
  37. if ((attr == null) || (attr.size() == 0)) {
  38. attrs = ictx.getAttributes(hostName, new String[] { "A" });
  39. attr = attrs.get("A");
  40. if (attr == null)
  41. throw new NamingException("No match for name '" + hostName + "'");
  42. }
  43. /*
  44. Huzzah! we have machines to try. Return them as an array list
  45. NOTE: We SHOULD take the preference into account to be absolutely
  46. correct. This is left as an exercise for anyone who cares.
  47. */
  48. ArrayList res = new ArrayList();
  49. NamingEnumeration en = attr.getAll();
  50.  
  51. while (en.hasMore()) {
  52. String mailhost;
  53. String x = (String) en.next();
  54. String f[] = x.split(" ");
  55. // THE fix *************
  56. if (f.length == 1)
  57. mailhost = f[0];
  58. else if (f[1].endsWith("."))
  59. mailhost = f[1].substring(0, (f[1].length() - 1));
  60. else
  61. mailhost = f[1];
  62. // THE fix *************
  63. res.add(mailhost);
  64. }
  65. return res;
  66. }
  67.  
  68. @SuppressWarnings("rawtypes")
  69. public static boolean isAddressValid(String address) {
  70. // Find the separator for the domain name
  71. int pos = address.indexOf('@');
  72.  
  73. // If the address does not contain an '@', it's not valid
  74. if (pos == -1)
  75. return false;
  76.  
  77. // Isolate the domain/machine name and get a list of mail exchangers
  78. String domain = address.substring(++pos);
  79. ArrayList mxList = null;
  80. try {
  81. mxList = getMX(domain);
  82. } catch (NamingException ex) {
  83. return false;
  84. }
  85.  
  86. /*
  87. Just because we can send mail to the domain, doesn't mean that the
  88. address is valid, but if we can't, it's a sure sign that it isn't
  89. */
  90. if (mxList.size() == 0)
  91. return false;
  92.  
  93. /*
  94. Now, do the SMTP validation, try each mail exchanger until we get
  95. a positive acceptance. It *MAY* be possible for one MX to allow
  96. a message [store and forwarder for example] and another [like
  97. the actual mail server] to reject it. This is why we REALLY ought
  98. to take the preference into account.
  99. */
  100. for (int mx = 0; mx < mxList.size(); mx++) {
  101. boolean valid = false;
  102. try {
  103. int res;
  104. //
  105. Socket skt = new Socket((String) mxList.get(mx), 25);
  106. BufferedReader rdr = new BufferedReader(new InputStreamReader(skt.getInputStream()));
  107. BufferedWriter wtr = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
  108.  
  109. res = hear(rdr);
  110. if (res != 220)
  111. throw new Exception("Invalid header");
  112. say(wtr, "EHLO rgagnon.com");
  113.  
  114. res = hear(rdr);
  115. if (res != 250)
  116. throw new Exception("Not ESMTP");
  117.  
  118. // validate the sender address
  119. say(wtr, "MAIL FROM: <tim@orbaker.com>");
  120. res = hear(rdr);
  121. if (res != 250)
  122. throw new Exception("Sender rejected");
  123.  
  124. say(wtr, "RCPT TO: <" + address + ">");
  125. res = hear(rdr);
  126.  
  127. // be polite
  128. say(wtr, "RSET");
  129. hear(rdr);
  130. say(wtr, "QUIT");
  131. hear(rdr);
  132. if (res != 250)
  133. throw new Exception("Address is not valid!");
  134.  
  135. valid = true;
  136. rdr.close();
  137. wtr.close();
  138. skt.close();
  139. } catch (Exception ex) {
  140. // Do nothing but try next host
  141. ex.printStackTrace();
  142. } finally {
  143. if (valid)
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149.  
  150. public static void main(String args[]) {
  151. String testData[] = { "rahul.saraswat@techblue.com", "rahul.saraswat@techblue.co.uk", "srswt.rahul12345@gmail.com",
  152. "srswt.rahul@gmail.com" };
  153. System.out.println(testData.length);
  154. for (int ctr = 0; ctr < testData.length; ctr++) {
  155. System.out.println(testData[ctr] + " is valid? " + isAddressValid(testData[ctr]));
  156. }
  157. return;
  158. }
  159. }
  160.  
  161. public class EmailTest {
  162. private static int hear(BufferedReader in) throws IOException {
  163. String line = null;
  164. int res = 0;
  165.  
  166. while ((line = in.readLine()) != null) {
  167. String pfx = line.substring(0, 3);
  168. try {
  169. res = Integer.parseInt(pfx);
  170. } catch (Exception ex) {
  171. res = -1;
  172. }
  173. if (line.charAt(3) != '-')
  174. break;
  175. }
  176.  
  177. return res;
  178. }
  179.  
  180. private static void say(BufferedWriter wr, String text) throws IOException {
  181. wr.write(text + "rn");
  182. wr.flush();
  183.  
  184. return;
  185. }
  186.  
  187. @SuppressWarnings({ "rawtypes", "unchecked" })
  188. private static ArrayList getMX(String hostName) throws NamingException {
  189. // Perform a DNS lookup for MX records in the domain
  190. Hashtable env = new Hashtable();
  191. env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
  192. DirContext ictx = new InitialDirContext(env);
  193. Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
  194. Attribute attr = attrs.get("MX");
  195.  
  196. // if we don't have an MX record, try the machine itself
  197. if ((attr == null) || (attr.size() == 0)) {
  198. attrs = ictx.getAttributes(hostName, new String[] { "A" });
  199. attr = attrs.get("A");
  200. if (attr == null)
  201. throw new NamingException("No match for name '" + hostName + "'");
  202. }
  203. /*
  204. Huzzah! we have machines to try. Return them as an array list
  205. NOTE: We SHOULD take the preference into account to be absolutely
  206. correct. This is left as an exercise for anyone who cares.
  207. */
  208. ArrayList res = new ArrayList();
  209. NamingEnumeration en = attr.getAll();
  210.  
  211. while (en.hasMore()) {
  212. String mailhost;
  213. String x = (String) en.next();
  214. String f[] = x.split(" ");
  215. // THE fix *************
  216. if (f.length == 1)
  217. mailhost = f[0];
  218. else if (f[1].endsWith("."))
  219. mailhost = f[1].substring(0, (f[1].length() - 1));
  220. else
  221. mailhost = f[1];
  222. // THE fix *************
  223. res.add(mailhost);
  224. }
  225. return res;
  226. }
  227.  
  228. @SuppressWarnings("rawtypes")
  229. public static boolean isAddressValid(String address) {
  230. // Find the separator for the domain name
  231. int pos = address.indexOf('@');
  232.  
  233. // If the address does not contain an '@', it's not valid
  234. if (pos == -1)
  235. return false;
  236.  
  237. // Isolate the domain/machine name and get a list of mail exchangers
  238. String domain = address.substring(++pos);
  239. ArrayList mxList = null;
  240. try {
  241. mxList = getMX(domain);
  242. } catch (NamingException ex) {
  243. return false;
  244. }
  245.  
  246. /*
  247. Just because we can send mail to the domain, doesn't mean that the
  248. address is valid, but if we can't, it's a sure sign that it isn't
  249. */
  250. if (mxList.size() == 0)
  251. return false;
  252.  
  253. /*
  254. Now, do the SMTP validation, try each mail exchanger until we get
  255. a positive acceptance. It *MAY* be possible for one MX to allow
  256. a message [store and forwarder for example] and another [like
  257. the actual mail server] to reject it. This is why we REALLY ought
  258. to take the preference into account.
  259. */
  260. for (int mx = 0; mx < mxList.size(); mx++) {
  261. boolean valid = false;
  262. try {
  263. int res;
  264. //
  265. Socket skt = new Socket((String) mxList.get(mx), 25);
  266. BufferedReader rdr = new BufferedReader(new InputStreamReader(skt.getInputStream()));
  267. BufferedWriter wtr = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
  268.  
  269. res = hear(rdr);
  270. if (res != 220)
  271. throw new Exception("Invalid header");
  272. say(wtr, "EHLO rgagnon.com");
  273.  
  274. res = hear(rdr);
  275. if (res != 250)
  276. throw new Exception("Not ESMTP");
  277.  
  278. // validate the sender address
  279. say(wtr, "MAIL FROM: <tim@orbaker.com>");
  280. res = hear(rdr);
  281. if (res != 250)
  282. throw new Exception("Sender rejected");
  283.  
  284. say(wtr, "RCPT TO: <" + address + ">");
  285. res = hear(rdr);
  286.  
  287. // be polite
  288. say(wtr, "RSET");
  289. hear(rdr);
  290. say(wtr, "QUIT");
  291. hear(rdr);
  292. if (res != 250)
  293. throw new Exception("Address is not valid!");
  294.  
  295. valid = true;
  296. rdr.close();
  297. wtr.close();
  298. skt.close();
  299. } catch (Exception ex) {
  300. // Do nothing but try next host
  301. ex.printStackTrace();
  302. } finally {
  303. if (valid)
  304. return true;
  305. }
  306. }
  307. return false;
  308. }
  309.  
  310. public static void main(String args[]) {
  311. String testData[] = { "rahul.saraswat@techblue.com", "rahul.saraswat@techblue.co.uk", "srswt.rahul12345@gmail.com",
  312. "srswt.rahul@gmail.com" };
  313. System.out.println(testData.length);
  314. for (int ctr = 0; ctr < testData.length; ctr++) {
  315. System.out.println(testData[ctr] + " is valid? " + isAddressValid(testData[ctr]));
  316. }
  317. return;
  318. }
  319. }
  320.  
  321. // Create a new instance of the EmailValidator class.
  322. EmailValidator em = new EmailValidator();
  323. em.MessageLogging += em_MessageLogging;
  324. em.EmailValidated += em_EmailValidationCompleted;
  325. try
  326. {
  327. string[] list = new string[3] { "test1@testdomain.com", "test2@testdomain.com", "test3@testdomain.com" };
  328. em.ValidateEmails(list);
  329. }
  330. catch (EmailValidatorException exc2)
  331. {
  332. Console.WriteLine("EmailValidatorException: " + exc2.Message);
  333. }
Add Comment
Please, Sign In to add comment