Advertisement
Guest User

Untitled

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