Advertisement
Guest User

Untitled

a guest
Mar 13th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. public static void main(String args[]) throws Exception {
  2. String host = "172.16.0.186";
  3. String user = "admin";
  4. String password = "1234";
  5. // Get system properties
  6. Properties properties = System.getProperties();
  7. // Get the default Session object.
  8. Session session = Session.getDefaultInstance(properties);
  9. // Get a Store object that implements the specified protocol.
  10. Store store = null;
  11. try {
  12. store = session.getStore("pop3");
  13. //Connect to the current host using the specified username and password.
  14. store.connect(host, user, password);
  15. } catch (Exception e) {
  16. logger.warn("An exception occured," + e);
  17. }
  18. //Create a Folder object corresponding to the given name.
  19. Folder folder = store.getFolder("Inbox");
  20. // Open the Folder.
  21. folder.open(Folder.READ_WRITE);
  22. Message[] message = folder.getMessages();
  23. for (int a = 0; a < message.length; a++) {
  24. System.out.println("-------------" + (a + 1) + "-----------");
  25. System.out.println(message[a].getSentDate());
  26. Multipart multipart = (Multipart) message[a].getContent();
  27. for (int i = 0; i < multipart.getCount(); i++) {
  28. //System.out.println(i);
  29. //System.out.println(multipart.getContentType());
  30. BodyPart bodyPart = multipart.getBodyPart(i);
  31. InputStream stream = bodyPart.getInputStream();
  32. File file = new File("C:\Attachments\" + bodyPart.getFileName());
  33. BufferedReader br = new BufferedReader(
  34. new InputStreamReader(stream));
  35. while (br.ready()) {
  36. System.out.println(br.readLine());
  37. }
  38. saveFile(bodyPart.getFileName(), stream);
  39. attachments.add(file);
  40. System.out.println();
  41. }
  42. System.out.println();
  43. }
  44. folder.close(true);
  45. store.close();
  46. }
  47.  
  48. /**
  49. * Save file.
  50. *
  51. * @param filename
  52. * the filename
  53. * @param input
  54. * the input
  55. */
  56. public static void saveFile(String filename, InputStream input) {
  57. System.out.println(filename);
  58. try {
  59. if (filename == null) {
  60. //filename = File.createTempFile("VSX", ".out").getName();
  61. return;
  62. }
  63. // Do no overwrite existing file
  64. filename = "C:\Attachments\" + filename;
  65. File file = new File(filename);
  66. for (int i = 0; file.exists(); i++) {
  67. file = new File(filename + i);
  68. }
  69. FileOutputStream fos = new FileOutputStream(file);
  70. BufferedOutputStream bos = new BufferedOutputStream(fos);
  71. BufferedInputStream bis = new BufferedInputStream(input);
  72. int aByte;
  73. while ((aByte = bis.read()) >= 0) {
  74. bos.write(aByte);
  75. }
  76. bos.flush();
  77. bos.close();
  78. bis.close();
  79. } // end of try()
  80. catch (IOException exp) {
  81. System.out.println("IOException:" + exp);
  82. }
  83. } //end of saveFile()
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement