Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. import javax.mail.*;
  2.  
  3. public class readEmails {
  4.  
  5. //Constructor Call
  6. public readEmails() {
  7. processMail();
  8. }
  9.  
  10. //Responsible for printing Data to Console
  11. private void printData(String data) {
  12. System.out.println(data);
  13. }
  14.  
  15. public void processMail() {
  16. Session session = null;
  17. Store store = null;
  18. Folder folder = null;
  19. Message message = null;
  20. Message[] messages = null;
  21. Object messagecontentObject = null;
  22. String sender = null;
  23. String subject = null;
  24. Multipart multipart = null;
  25. Part part = null;
  26. String contentType = null;
  27.  
  28. try {
  29. printData("--------------processing mails started-----------------");
  30. session = Session.getDefaultInstance(System.getProperties(), null);
  31.  
  32. printData("getting the session for accessing email.");
  33. store = session.getStore("imap");
  34.  
  35. store.connect("host Name OR IP","username","password");
  36. printData("Connection established with IMAP server.");
  37.  
  38. // Get a handle on the default folder
  39. folder = store.getDefaultFolder();
  40.  
  41. printData("Getting the Inbox folder.");
  42.  
  43. // Retrieve the "Inbox"
  44. folder = folder.getFolder("inbox");
  45.  
  46. //Reading the Email Index in Read / Write Mode
  47. folder.open(Folder.READ_WRITE);
  48.  
  49. // Retrieve the messages
  50. messages = folder.getMessages();
  51.  
  52. // Loop over all of the messages
  53. for (int messageNumber = 0; messageNumber < messages.length; messageNumber++) {
  54. // Retrieve the next message to be read
  55. message = messages[messageNumber];
  56.  
  57. // Retrieve the message content
  58. messagecontentObject = message.getContent();
  59.  
  60. // Determine email type
  61. if (messagecontentObject instanceof Multipart) {
  62. printData("Found Email with Attachment");
  63. sender = ((InternetAddress) message.getFrom()[0]).getPersonal();
  64.  
  65. // If the "personal" information has no entry, check the address for the sender information
  66. printData("If the personal information has no entry, check the address for the sender information.");
  67.  
  68. if (sender == null) {
  69. sender = ((InternetAddress) message.getFrom()[0]).getAddress();
  70. printData("sender in NULL. Printing Address:" + sender);
  71. }
  72. printData("Sender -." + sender);
  73.  
  74. // Get the subject information
  75. subject = message.getSubject();
  76.  
  77. printData("subject=" + subject);
  78.  
  79. // Retrieve the Multipart object from the message
  80. multipart = (Multipart) message.getContent();
  81.  
  82. printData("Retrieve the Multipart object from the message");
  83.  
  84. // Loop over the parts of the email
  85. for (int i = 0; i < multipart.getCount(); i++) {
  86. // Retrieve the next part
  87. part = multipart.getBodyPart(i);
  88.  
  89. // Get the content type
  90. contentType = part.getContentType();
  91.  
  92. // Display the content type
  93. printData("Content: " + contentType);
  94.  
  95. if (contentType.startsWith("text/plain")) {
  96. printData("---------reading content type text/plain mail -------------");
  97. } else {
  98. // Retrieve the file name
  99. String fileName = part.getFileName();
  100. printData("retrive the fileName="+ fileName);
  101. }
  102. }
  103. } else {
  104. printData("Found Mail Without Attachment");
  105. sender = ((InternetAddress) message.getFrom()[0]).getPersonal();
  106.  
  107. // If the "personal" information has no entry, check the address for the sender information
  108. printData("If the personal information has no entry, check the address for the sender information.");
  109.  
  110. if (sender == null) {
  111. sender = ((InternetAddress) message.getFrom()[0]).getAddress();
  112. printData("sender in NULL. Printing Address:" + sender);
  113. }
  114.  
  115. // Get the subject information
  116. subject = message.getSubject();
  117. printData("subject=" + subject);
  118. }
  119. }
  120.  
  121. // Close the folder
  122. folder.close(true);
  123.  
  124. // Close the message store
  125. store.close();
  126. } catch (Exception e) {
  127. printData("Not able to process the mail reading.");
  128. e.printStackTrace();
  129. }
  130. }
  131.  
  132. //Main Function for The readEmail Class
  133. public static void main(String args[]) {
  134. //Creating new readEmail Object
  135. readEmails readMail = new readEmails();
  136.  
  137. //Calling processMail Function to read from IMAP Account
  138. readMail.processMail();
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement