Advertisement
Guest User

FetchEmail

a guest
Aug 26th, 2018
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. import java.io.BufferedOutputStream;
  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.util.Date;
  10. import java.util.Properties;
  11.  
  12. import javax.mail.Address;
  13. import javax.mail.Folder;
  14. import javax.mail.Message;
  15. import javax.mail.MessagingException;
  16. import javax.mail.Multipart;
  17. import javax.mail.NoSuchProviderException;
  18. import javax.mail.Part;
  19. import javax.mail.Session;
  20. import javax.mail.Store;
  21.  
  22. public class FetchingEmail {
  23.  
  24. public static void fetch(String pop3Host, String storeType, String user,
  25. String password) {
  26. try {
  27. // create properties field
  28. Properties properties = new Properties();
  29. properties.put("mail.store.protocol", "pop3");
  30. properties.put("mail.pop3.host", pop3Host);
  31. properties.put("mail.pop3.port", "995");
  32. properties.put("mail.pop3.starttls.enable", "true");
  33. Session emailSession = Session.getDefaultInstance(properties);
  34. // emailSession.setDebug(true);
  35.  
  36. // create the POP3 store object and connect with the pop server
  37. Store store = emailSession.getStore("pop3s");
  38.  
  39. store.connect(pop3Host, user, password);
  40.  
  41. // create the folder object and open it
  42. Folder emailFolder = store.getFolder("INBOX");
  43. emailFolder.open(Folder.READ_ONLY);
  44.  
  45. BufferedReader reader = new BufferedReader(new InputStreamReader(
  46. System.in));
  47.  
  48. // retrieve the messages from the folder in an array and print it
  49. Message[] messages = emailFolder.getMessages();
  50. System.out.println("messages.length---" + messages.length);
  51.  
  52. for (int i = 0; i < messages.length; i++) {
  53. Message message = messages[i];
  54. System.out.println("---------------------------------");
  55. writePart(message);
  56. String line = reader.readLine();
  57. if ("YES".equals(line)) {
  58. message.writeTo(System.out);
  59. } else if ("QUIT".equals(line)) {
  60. break;
  61. }
  62. }
  63.  
  64. // close the store and folder objects
  65. emailFolder.close(false);
  66. store.close();
  67.  
  68. } catch (NoSuchProviderException e) {
  69. e.printStackTrace();
  70. } catch (MessagingException e) {
  71. e.printStackTrace();
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. public static void main(String[] args) {
  79.  
  80. String host = "pop.gmail.com";// change accordingly
  81. String mailStoreType = "pop3";
  82. String username =
  83. "abc@gmail.com";// change accordingly
  84. String password = "*****";// change accordingly
  85.  
  86. //Call method fetch
  87. fetch(host, mailStoreType, username, password);
  88.  
  89. }
  90.  
  91. /*
  92. * This method checks for content-type
  93. * based on which, it processes and
  94. * fetches the content of the message
  95. */
  96. public static void writePart(Part p) throws Exception {
  97. if (p instanceof Message)
  98. //Call methos writeEnvelope
  99. writeEnvelope((Message) p);
  100.  
  101. System.out.println("----------------------------");
  102. System.out.println("CONTENT-TYPE: " + p.getContentType());
  103.  
  104. //check if the content is plain text
  105. if (p.isMimeType("text/plain")) {
  106. System.out.println("This is plain text");
  107. System.out.println("---------------------------");
  108. System.out.println((String) p.getContent());
  109. }
  110. //check if the content has attachment
  111. else if (p.isMimeType("multipart/*")) {
  112. System.out.println("This is a Multipart");
  113. System.out.println("---------------------------");
  114. Multipart mp = (Multipart) p.getContent();
  115. int count = mp.getCount();
  116. for (int i = 0; i < count; i++)
  117. writePart(mp.getBodyPart(i));
  118. }
  119. //check if the content is a nested message
  120. else if (p.isMimeType("message/rfc822")) {
  121. System.out.println("This is a Nested Message");
  122. System.out.println("---------------------------");
  123. writePart((Part) p.getContent());
  124. }
  125. //check if the content is an inline image
  126. else if (p.isMimeType("image/jpeg")) {
  127. System.out.println("--------> image/jpeg");
  128. Object o = p.getContent();
  129.  
  130. InputStream x = (InputStream) o;
  131. // Construct the required byte array
  132. System.out.println("x.length = " + x.available());
  133. while ((i = (int) ((InputStream) x).available()) > 0) {
  134. int result = (int) (((InputStream) x).read(bArray));
  135. if (result == -1)
  136. int i = 0;
  137. byte[] bArray = new byte[x.available()];
  138.  
  139. break;
  140. }
  141. FileOutputStream f2 = new FileOutputStream("/tmp/image.jpg");
  142. f2.write(bArray);
  143. }
  144. else if (p.getContentType().contains("image/")) {
  145. System.out.println("content type" + p.getContentType());
  146. File f = new File("image" + new Date().getTime() + ".jpg");
  147. DataOutputStream output = new DataOutputStream(
  148. new BufferedOutputStream(new FileOutputStream(f)));
  149. com.sun.mail.util.BASE64DecoderStream test =
  150. (com.sun.mail.util.BASE64DecoderStream) p
  151. .getContent();
  152. byte[] buffer = new byte[1024];
  153. int bytesRead;
  154. while ((bytesRead = test.read(buffer)) != -1) {
  155. output.write(buffer, 0, bytesRead);
  156. }
  157. }
  158. else {
  159. Object o = p.getContent();
  160. if (o instanceof String) {
  161. System.out.println("This is a string");
  162. System.out.println("---------------------------");
  163. System.out.println((String) o);
  164. }
  165. else if (o instanceof InputStream) {
  166. System.out.println("This is just an input stream");
  167. System.out.println("---------------------------");
  168. InputStream is = (InputStream) o;
  169. is = (InputStream) o;
  170. int c;
  171. while ((c = is.read()) != -1)
  172. System.out.write(c);
  173. }
  174. else {
  175. System.out.println("This is an unknown type");
  176. System.out.println("---------------------------");
  177. System.out.println(o.toString());
  178. }
  179. }
  180.  
  181. }
  182. /*
  183. * This method would print FROM,TO and SUBJECT of the message
  184. */
  185. public static void writeEnvelope(Message m) throws Exception {
  186. System.out.println("This is the message envelope");
  187. System.out.println("---------------------------");
  188. Address[] a;
  189.  
  190. // FROM
  191. if ((a = m.getFrom()) != null) {
  192. for (int j = 0; j < a.length; j++)
  193. System.out.println("FROM: " + a[j].toString());
  194. }
  195.  
  196. // TO
  197. if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
  198. for (int j = 0; j < a.length; j++)
  199. System.out.println("TO: " + a[j].toString());
  200. }
  201.  
  202. // SUBJECT
  203. if (m.getSubject() != null)
  204. System.out.println("SUBJECT: " + m.getSubject());
  205.  
  206. }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement