Advertisement
Guest User

Untitled

a guest
Jan 14th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. public void checkInboxEmail(final String host, final String user, final String password) {
  2.  
  3. Log.d(TAG, "checkEmail");
  4.  
  5. this.host = host;
  6. this.user = user;
  7. this.password = password;
  8.  
  9. new Thread(new Runnable() {
  10. @Override
  11. public void run() {
  12. try {
  13. Log.d(TAG, "checkEmail - run()");
  14.  
  15. long databaseRecords;
  16.  
  17. //create properties field
  18. Properties properties = new Properties();
  19. properties.put("mail.store.protocol", "imaps");
  20. properties.put("mail.imaps.ssl.trust", "*");
  21.  
  22. emailSession = Session.getInstance(properties, new Authenticator() {
  23. protected PasswordAuthentication getPasswordAuthentication() {
  24. return new PasswordAuthentication(user, password);
  25. }
  26. });
  27.  
  28.  
  29. IMAPStore imapStore = (IMAPStore) emailSession.getStore("imaps");
  30. // imapStore.connect();
  31.  
  32. imapStore.connect(host, user, password);
  33.  
  34. if (imapStore.isConnected()) {
  35. Log.d("MailPush", "Successfully connected to IMAP");
  36. } else {
  37. Log.d("MailPush", "Not connected to IMAP");
  38. }
  39.  
  40. final IMAPFolder folder = (IMAPFolder) imapStore.getFolder("Inbox");
  41. folder.open(IMAPFolder.READ_WRITE);
  42.  
  43. databaseRecords = dbManager.getReceivedEmailRecordsCount();
  44.  
  45. if (databaseRecords < folder.getMessageCount()) {
  46. Log.d(TAG, "Receiving Mail...");
  47. receiveMail(folder.getMessages());
  48. } else {
  49. Log.d(TAG, "Records match.");
  50. }
  51.  
  52. Folder[] fdr = imapStore.getDefaultFolder().list();
  53. for (Folder fd : fdr)
  54. System.out.println(">> " + fd.getName());
  55.  
  56. folder.addMessageCountListener(new MessageCountListener() {
  57.  
  58. public void messagesAdded(MessageCountEvent e) {
  59.  
  60. System.out.println("Message Added Event Fired");
  61. Log.d(TAG, "MESSAGE TYPE: " + e.getType());
  62. //ADDED = 1 & REMOVED = 2
  63.  
  64. try {
  65. Message[] messages = e.getMessages();
  66.  
  67. System.out.println("messages.length---" + messages.length);
  68.  
  69. for (Message message : messages) {
  70. if (!message.getFlags().contains(Flags.Flag.SEEN)) {
  71.  
  72. //Message is new (hasn't been seen) > Message Details
  73. System.out.println("---------------------------------");
  74. System.out.println("Email Number " + (message.getMessageNumber()));
  75. System.out.println("Subject: " + message.getSubject());
  76. System.out.println("From: " + message.getFrom()[0]);
  77. System.out.println("Text: " + message.getContent().toString());
  78.  
  79. String from = message.getFrom()[0].toString();
  80.  
  81. String cc = InternetAddress.toString(message.getRecipients(Message.RecipientType.CC));
  82. Log.d(TAG, "CC 1: " + cc);
  83.  
  84. Address[] recipients = message.getRecipients(Message.RecipientType.CC);
  85. cc = InternetAddress.toString(recipients);
  86. Log.d(TAG, "CC 2: " + cc);
  87.  
  88. //Check Encryption Details > Add SEEN Flag > Add to database
  89. checkEncryption((MimeMessage) message, from, cc);
  90. }
  91. }
  92. } catch (Exception ex) {
  93. ex.printStackTrace();
  94. }
  95. }
  96.  
  97. public void messagesRemoved(MessageCountEvent e) {
  98. System.out.println("Message Removed Event fired");
  99. }
  100. });
  101.  
  102. folder.addMessageChangedListener(new MessageChangedListener() {
  103.  
  104. public void messageChanged(MessageChangedEvent e) {
  105. System.out.println("Message Changed Event fired");
  106. }
  107. });
  108.  
  109. startListening(folder);
  110.  
  111. //close the store and folder objects
  112. // emailFolder.close(false);
  113. // store.close();
  114.  
  115. } catch (NoSuchProviderException e) {
  116. e.printStackTrace();
  117. } catch (MessagingException e) {
  118. e.printStackTrace();
  119. } catch (Exception e) {
  120. e.printStackTrace();
  121. }
  122. }
  123. }).start();
  124. }
  125.  
  126. private void startListening(IMAPFolder imapFolder) {
  127. Log.d(TAG, "startListening");
  128.  
  129. // We need to create a new thread to keep alive the connection
  130. Thread t = new Thread(
  131. new KeepAliveRunnable(imapFolder), "IdleConnectionKeepAlive"
  132. );
  133.  
  134. t.start();
  135.  
  136. while (!Thread.interrupted()) {
  137. Log.d(TAG, "Starting IDLE");
  138. try {
  139. Log.d(TAG, "Setting IDLE");
  140. imapFolder.idle();
  141. } catch (FolderClosedException fex) {
  142. //Server closes connection.
  143. Log.d(TAG, "FolderClosedException. Server potentially dropped connection. Retrying connection...");
  144. fex.printStackTrace();
  145.  
  146. if (!isServiceRunning(MyService.class)) {
  147. Log.d(TAG, "Service isn't running. Starting service...");
  148.  
  149. //Start service
  150. Intent intent = new Intent(context, MyService.class);
  151. intent.putExtra("host", host);
  152. intent.putExtra("email", user);
  153. intent.putExtra("password", password);
  154.  
  155. context.startService(intent);
  156. } else {
  157. Log.d(TAG, "Service is already running. Checking email...");
  158. checkInboxEmail(host, user, password);
  159. }
  160. } catch (MessagingException e) {
  161. //Idle isn't supported by server.
  162. Log.d(TAG, "Messaging exception during IDLE: ");
  163. e.printStackTrace();
  164. }
  165. }
  166.  
  167. // Shutdown keep alive thread
  168. if (t.isAlive()) {
  169. Log.d(TAG, "Interrupting thread");
  170. t.interrupt();
  171. }
  172. }
  173.  
  174. private static class KeepAliveRunnable implements Runnable {
  175.  
  176. private final String TAG = getClass().getName();
  177.  
  178. private static final long KEEP_ALIVE_FREQ = 60000 * 10; // 15 minutes (Exchange connection drops after 20-30 minutes)
  179.  
  180. private IMAPFolder folder;
  181.  
  182. KeepAliveRunnable(IMAPFolder folder) {
  183. this.folder = folder;
  184. }
  185.  
  186. @Override
  187. public void run() {
  188. while (!Thread.interrupted()) {
  189. try {
  190. Thread.sleep(KEEP_ALIVE_FREQ);
  191.  
  192. // Perform a NOOP just to keep alive the connection
  193. Log.d(TAG, "Performing a NOOP to keep the connection alive");
  194. folder.doCommand(new IMAPFolder.ProtocolCommand() {
  195. public Object doCommand(IMAPProtocol p)
  196. throws ProtocolException {
  197. p.simpleCommand("NOOP", null);
  198. return null;
  199. }
  200. });
  201. } catch (InterruptedException e) {
  202. // Ignore, just aborting the thread...
  203. Log.d(TAG, "Interrupted...");
  204. e.printStackTrace();
  205. } catch (MessagingException e) {
  206. // Shouldn't really happen...
  207. Log.d(TAG, "Unexpected exception while keeping alive the IDLE connection");
  208. e.printStackTrace();
  209. }
  210. }
  211. }
  212. }
  213.  
  214. private void receiveMail(Message[] messages) {
  215. try {
  216.  
  217. System.out.println("messages.length---" + messages.length);
  218.  
  219. for (Message message : messages) {
  220. if (!message.getFlags().contains(Flags.Flag.SEEN)) {
  221.  
  222. //Message is new (hasn't been seen) > Message Details
  223. System.out.println("---------------------------------");
  224. System.out.println("Email Number " + (message.getMessageNumber()));
  225. System.out.println("Subject: " + message.getSubject());
  226. System.out.println("From: " + message.getFrom()[0]);
  227. System.out.println("Text: " + message.getContent().toString());
  228.  
  229. String from = message.getFrom()[0].toString();
  230. String cc = InternetAddress.toString(message.getRecipients(Message.RecipientType.CC));
  231.  
  232. //Check Encryption Details > Add SEEN Flag > Add to database
  233. checkEncryption((MimeMessage) message, from, cc);
  234. }
  235. }
  236. } catch (Exception ex) {
  237. ex.printStackTrace();
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement