/src/pallettown/EmailVerifier.java

https://github.com/novskey/PalletTown · Java · 197 lines · 125 code · 53 blank · 19 comment · 23 complexity · 1229cc3770ad49c9fce7fe5d49fe7222 MD5 · raw file

  1. package pallettown;
  2. import javax.mail.*;
  3. import javax.mail.search.SearchTerm;
  4. import java.util.Properties;
  5. import static pallettown.GUI.Log;
  6. /**
  7. * Created by Paris on 20/01/2017.
  8. */
  9. class EmailVerifier {
  10. private static final Properties mailServerProperties = new Properties();
  11. private static Folder trash;
  12. private static final String GMAIL_HOST = "imap.gmail.com";
  13. // private static final String GMAIL_PORT = "993";
  14. private static final String HOTMAIL_HOST = "imap-mail.outlook.com";
  15. // private static final String HOTMAIL_PORT = ""
  16. private static final String ZOHO_HOST = "imap.zoho.com";
  17. private static Folder inbox;
  18. private static final Flags deleted = new Flags(Flags.Flag.DELETED);
  19. static String host;
  20. public static void verify(String email, String emailPass, int accounts, int retries) {
  21. // String port;
  22. if (email.contains("@gmail.com")) {
  23. host = GMAIL_HOST;
  24. // port = "993";
  25. } else if (email.contains("@hotmail.")) {
  26. host = HOTMAIL_HOST;
  27. } else if (email.contains("@zoho.")) {
  28. host = ZOHO_HOST;
  29. } else {
  30. Log("invalid email, please use hotmail, gmail, or zoho");
  31. return;
  32. }
  33. mailServerProperties.put("mail.imap.host", host);
  34. mailServerProperties.put("mail.imap.port", "993");
  35. mailServerProperties.put("mail.imap.starttls.enable", "true");
  36. Session getMailSession = Session.getDefaultInstance(mailServerProperties, null);
  37. Store store = null;
  38. try {
  39. store = getMailSession.getStore("imaps");
  40. store.connect(host, email, emailPass);
  41. // opens the inbox folder
  42. inbox = store.getFolder("INBOX");
  43. inbox.open(Folder.READ_WRITE);
  44. if (host.equals(GMAIL_HOST))
  45. trash = store.getFolder("[Gmail]/" + PalletTown.trashName);
  46. else if (host.equals(ZOHO_HOST))
  47. trash = store.getFolder("Trash");
  48. else
  49. trash = store.getFolder("Deleted");
  50. trash.open(Folder.READ_WRITE);
  51. Log("logged in to: " + email);
  52. // creates a search criterion
  53. SearchTerm searchCondition = new SearchTerm() {
  54. @Override
  55. public boolean match(Message message) {
  56. try {
  57. if (message.getSubject().contains("Activation")) {
  58. return true;
  59. }
  60. } catch (MessagingException ex) {
  61. ex.printStackTrace();
  62. }
  63. return false;
  64. }
  65. };
  66. // performs search through the folder
  67. Message[] foundMessages = inbox.search(searchCondition);
  68. processMail(foundMessages);
  69. if (foundMessages.length < accounts) {
  70. //max 5 retries
  71. Log("Not all verification emails received in time");
  72. Log("Waiting another 3 minutes then trying again");
  73. inbox.close(true);
  74. store.close();
  75. Thread.sleep(180000);
  76. if (retries > 0)
  77. verify(email, emailPass, accounts - foundMessages.length, retries - 1);
  78. }
  79. } catch (MessagingException | InterruptedException e) {
  80. e.printStackTrace();
  81. } finally {
  82. try {
  83. inbox.close(true);
  84. trash.close(true);
  85. store.close();
  86. } catch (MessagingException e) {
  87. e.printStackTrace();
  88. }
  89. }
  90. }
  91. private static void processMail(Message[] foundMessages) {
  92. Log("Processing " + foundMessages.length + " emails");
  93. if (foundMessages.length == 0) {
  94. Log("no emails found");
  95. return;
  96. }
  97. for (Message message : foundMessages) {
  98. try {
  99. // assert message.getFrom().equals("noreply@pokemon.com");
  100. String content = getMailText(message);
  101. int validkey_index = content.indexOf("https://club.pokemon.com/us/pokemon-trainer-club/activated/");
  102. if (validkey_index != -1) {
  103. String validlink = content.substring(validkey_index, validkey_index + 93);
  104. validlink = validlink.replace("\r", "");
  105. validlink = validlink.replace("\n", "");
  106. validlink = validlink.replace(">", "");
  107. validlink = validlink.replace("=", "");
  108. Log(validlink);
  109. String validate_response = "Failed";
  110. while (validate_response.equals("Failed")) {
  111. validate_response = UrlUtil.openUrl(validlink);
  112. }
  113. Log(validate_response);
  114. Log("Verified email and trashing, validate key: " + validlink.substring(60) + "\n");
  115. Message[] messageArr = new Message[]{message};
  116. if (!host.equals(ZOHO_HOST))
  117. inbox.copyMessages(messageArr, trash);
  118. inbox.setFlags(messageArr, deleted, true);
  119. }
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. }
  125. /*
  126. * This method checks for content-type
  127. * based on which, it processes and
  128. * fetches the content of the message
  129. */
  130. private static String getMailText(Part p) throws Exception {
  131. //check if the content is plain text
  132. if (p.isMimeType("text/plain")) {
  133. return ((String) p.getContent());
  134. }
  135. //check if the content has attachment
  136. else if (p.isMimeType("multipart/*")) {
  137. Multipart mp = (Multipart) p.getContent();
  138. int count = mp.getCount();
  139. for (int i = 0; i < count; i++)
  140. return getMailText(mp.getBodyPart(i));
  141. }
  142. return "";
  143. }
  144. }