Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. String user = "mail@mail.com";
  2. String password = "mypassword";
  3. try{
  4.  
  5. boolean foundSites = false;
  6.  
  7. // Get a Properties object
  8. Properties properties = new Properties();
  9.  
  10. properties.put("mail.imaps.host", host);
  11. properties.put("mail.imaps.port", port);
  12. properties.put("mail.imaps.starttls.enable", SSL);
  13. //properties.put("mail.imap.fetchsize", "1000000");
  14. Session emailSession = Session.getDefaultInstance(properties);
  15.  
  16. //create the POP3 store object and connect with the pop server
  17. Store store = emailSession.getStore("imaps");
  18. store.connect(host, user, password);
  19.  
  20. //create the folder object and open it
  21. Folder emailFolder = store.getFolder("INBOX");
  22. emailFolder.open(Folder.READ_ONLY);
  23.  
  24. // retrieve the messages from the folder in an array and print it
  25. Message[] messages = emailFolder.getMessages();
  26. System.out.println("messages.length---" + messages.length);
  27.  
  28. LocalDateTime now = LocalDateTime.now();
  29. int year = now.getYear();
  30. int month = now.getMonthValue();
  31. int day = now.getDayOfMonth();
  32.  
  33. for (int i = messages.length - 1; i >= 0; i--) {
  34. Message message = messages[i];
  35.  
  36.  
  37. Calendar cal = Calendar.getInstance();
  38. cal.setTime(message.getReceivedDate());
  39. int yearMessage = cal.get(Calendar.YEAR);
  40. int monthMessage = cal.get(Calendar.MONTH) + 1;
  41. int dayMessage = cal.get(Calendar.DAY_OF_MONTH);
  42.  
  43.  
  44.  
  45. if(year == yearMessage && month == monthMessage && day == dayMessage)
  46. {
  47. //The real code is doing this with 20 Subjects (20 emails)
  48. if(message.getSubject().equalsIgnoreCase("Integration - Site Info") && foundSites != true)
  49. {
  50. System.out.println("found Integration - Site Info");
  51.  
  52. Multipart multipart = (Multipart) message.getContent();
  53. List<File> attachments = new ArrayList<>();
  54. for (int j = 0; j < multipart.getCount(); j++) {
  55. BodyPart bodyPart = multipart.getBodyPart(j);
  56. if(Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()))
  57. {
  58. InputStream is = bodyPart.getInputStream();
  59. File f = new File("./attachments/"
  60. + "sites_"+Integer.toString(day)+"-"+Integer.toString(month)+"-"+Integer.toString(year)+".csv");
  61. FileOutputStream fos = new FileOutputStream(f);
  62. byte[] buf = new byte[4096];
  63. int bytesRead;
  64. while((bytesRead = is.read(buf))!=-1) {
  65. fos.write(buf, 0, bytesRead);
  66. }
  67. fos.close();
  68. attachments.add(f);
  69. foundSites = true;
  70. break;
  71. }
  72.  
  73.  
  74. }
  75. }
  76. }
  77.  
  78. if(foundSites)
  79. {
  80. break;
  81. }
  82. } catch (Exception e) {
  83. System.out.println(e);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement