Guest User

Untitled

a guest
Jul 29th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. package com.emailreading;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.mail.Address;
  6. import javax.mail.Folder;
  7. import javax.mail.Message;
  8. import javax.mail.MessagingException;
  9. import javax.mail.Multipart;
  10. import javax.mail.Part;
  11. import javax.mail.Session;
  12. import javax.mail.Store;
  13. import javax.mail.internet.MimeBodyPart;
  14. import javax.mail.search.SearchTerm;
  15.  
  16. public class EmailAttachReceiver {
  17.  
  18. String fileName;
  19. public void downloadEmailAttachments(String userName, String password) {
  20.  
  21. try {
  22. //Code used to connect to a gmail account
  23. //Starts Here
  24. Properties props = new Properties();
  25. props.setProperty("mail.store.protocol", "imaps");
  26. Session session = Session.getInstance(props, null);
  27. Store store = session.getStore();
  28. store.connect("imap.gmail.com", userName, password);
  29. Folder folderInbox = store.getFolder("INBOX");
  30. folderInbox.open(Folder.READ_ONLY);
  31. String RetailerName="Sudha Medicals";
  32. // Ends Here
  33.  
  34. //Code used to search particular parameter from the email.
  35. //Starts Here
  36. SearchTerm searchCondition = new SearchTerm() {
  37. @Override
  38. public boolean match(Message message) {
  39. try {
  40. if (message.getSubject().contains("Sudha Medicals Reports")) {
  41. return true;
  42. }
  43. } catch (MessagingException ex) {
  44. ex.printStackTrace();
  45. }
  46. return false;
  47. }
  48. };
  49. //Ends Here.
  50.  
  51. //Loop iteration based on matching parameters given in search method.
  52.  
  53. //Starts Here
  54. Message[] arrayMessages = folderInbox.search(searchCondition);
  55. System.out.println(RetailerName);
  56. System.out.println("Total Email Count "+arrayMessages.length);
  57. for (int i = 0; i < arrayMessages.length; i++) {
  58. //i=arrayMessages.length-1;
  59. Message message = arrayMessages[i];
  60. //Address[] fromAddress = message.getFrom();
  61. //String from = fromAddress[0].toString();
  62. String subject = message.getSubject();
  63. // String sentDate = message.getSentDate().toString();
  64. String contentType = message.getContentType();
  65.  
  66. String messageContent = "";
  67. String attachFiles = "";
  68.  
  69. //Code to handle the multipart text in email body.
  70. //Starts Here
  71. if (contentType.contains("multipart")) {
  72. // System.out.println("contentType 1 "+contentType);
  73. Multipart multiPart = (Multipart) message.getContent();
  74. int numberOfParts = multiPart.getCount();
  75. // System.out.println("numberOfParts "+numberOfParts);
  76. for (int partCount = 0; partCount < numberOfParts; partCount++) {
  77. MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
  78. if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
  79. // System.out.println("partCount 1 " + partCount);
  80. } else {
  81. // System.out.println("partCount 2 "+partCount);
  82. messageContent = part.getContent().toString();
  83. }
  84. }
  85.  
  86. } else if (contentType.contains("text/plain")
  87. || contentType.contains("text/html")) {
  88. //System.out.println("contentType 2 " + contentType);
  89. Object content = message.getContent();
  90. if (content != null) {
  91. messageContent = content.toString();
  92. }
  93. }
  94. //Ends Here
  95. /* Code To Replace The HTML Tags From The Content */
  96. //Starts Here
  97. String noHTMLString = messageContent.replaceAll("\\<.*?>","");
  98.  
  99. String str = noHTMLString;
  100.  
  101. String delimiter = "Total stock value";
  102.  
  103. String[] temp = str.split(delimiter);
  104. System.out.println("Message #" + (i + 1) + ":");
  105. //System.out.println("\t Subject: " + subject );
  106.  
  107. String[] temp1 = temp.length>1?temp[1].split("Stock adjustments done"):temp;
  108. if(temp1[0].contains(": ₹ ")){
  109. System.out.println("\t Subject: " + subject + " Total Stock Value "+ temp1[0].replace(": ₹ ", ""));
  110. //System.out.println( subject + " "+temp1[0].replace(": ₹ ", ""));
  111. //System.out.println("Total Stock Value:"+temp1[0].replace(": ₹ ", ""));
  112. }
  113. }
  114.  
  115. folderInbox.close(false);
  116. store.close();
  117. } catch (Exception ex) {
  118. ex.printStackTrace();
  119. }
  120. }
  121.  
  122. //Ends Here
  123.  
  124. public static void main(String[] args) {
  125.  
  126. String userName = "abc@gmail.com";
  127. String password = "PSWD";
  128. EmailAttachReceiver receiver = new EmailAttachReceiver();
  129. receiver.downloadEmailAttachments(userName, password);
  130.  
  131. }
  132. }
Add Comment
Please, Sign In to add comment