Guest User

Untitled

a guest
Mar 24th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. javax.mail.MessagingException: Connect failed;
  2. nested exception is:
  3. javax.net.ssl.SSLHandshakeException:
  4. sun.security.validator.ValidatorException: PKIX path building failed:
  5. sun.security.provider.certpath.SunCertPathBuilderException: unable to find
  6. valid certification path to requested target
  7.  
  8.  
  9.  
  10. package emailattachmentsdownloader;
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.util.Properties;
  14. import javax.mail.Address;
  15. import javax.mail.Folder;
  16. import javax.mail.Message;
  17. import javax.mail.MessagingException;
  18. import javax.mail.Multipart;
  19. import javax.mail.NoSuchProviderException;
  20. import javax.mail.Part;
  21. import javax.mail.Session;
  22. import javax.mail.Store;
  23. import javax.mail.internet.MimeBodyPart;
  24. /**
  25. * This program demonstrates how to download e-mail messages and save
  26. * attachments into files on disk.
  27. *
  28. *
  29. *
  30. */
  31. public class EmailAttachmentsDownloader {
  32. private String saveDirectory;
  33. /**
  34. * Sets the directory where attached files will be stored.
  35. * @param dir absolute path of the directory
  36. */
  37. public void setSaveDirectory(String dir) {
  38. this.saveDirectory = dir;
  39. }
  40. /**
  41. * Downloads new messages and saves attachments to disk if any.
  42. * @param host
  43. * @param port
  44. * @param userName
  45. * @param password
  46. */
  47. public void downloadEmailAttachments(String host, String port,
  48. String userName, String password) {
  49. Properties properties = new Properties();
  50.  
  51. // server setting
  52. properties.put("mail.pop3.host", host);
  53. properties.put("mail.pop3.port", port);
  54.  
  55. // SSL setting
  56. properties.setProperty("mail.pop3.socketFactory.class",
  57. "javax.net.ssl.SSLSocketFactory");
  58. properties.setProperty("mail.pop3.socketFactory.fallback", "false");
  59. properties.setProperty("mail.pop3.socketFactory.port",
  60. String.valueOf(port));
  61.  
  62. Session session = Session.getDefaultInstance(properties);
  63.  
  64. try {
  65. // connects to the message store
  66. Store store = session.getStore("pop3");
  67. store.connect(userName, password);
  68.  
  69. // opens the inbox folder
  70. Folder folderInbox = store.getFolder("INBOX");
  71. folderInbox.open(Folder.READ_ONLY);
  72.  
  73. // fetches new messages from server
  74. Message[] arrayMessages = folderInbox.getMessages();
  75.  
  76. for (int i = 0; i < arrayMessages.length; i++) {
  77. Message message = arrayMessages[i];
  78. Address[] fromAddress = message.getFrom();
  79. String from = fromAddress[0].toString();
  80. String subject = message.getSubject();
  81. String sentDate = message.getSentDate().toString();
  82.  
  83. String contentType = message.getContentType();
  84. String messageContent = "";
  85.  
  86. // store attachment file name, separated by comma
  87. String attachFiles = "";
  88.  
  89. if (contentType.contains("multipart")) {
  90. // content may contain attachments
  91. Multipart multiPart = (Multipart) message.getContent();
  92. int numberOfParts = multiPart.getCount();
  93. for (int partCount = 0; partCount < numberOfParts; partCount++) {
  94. MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
  95. if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
  96. // this part is attachment
  97. String fileName = part.getFileName();
  98. attachFiles += fileName + ", ";
  99. part.saveFile(saveDirectory + File.separator + fileName);
  100. } else {
  101. // this part may be the message content
  102. messageContent = part.getContent().toString();
  103. }
  104. }
  105.  
  106. if (attachFiles.length() > 1) {
  107. attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
  108. }
  109. } else if (contentType.contains("text/plain")
  110. || contentType.contains("text/html")) {
  111. Object content = message.getContent();
  112. if (content != null) {
  113. messageContent = content.toString();
  114. }
  115. }
  116.  
  117. // print out details of each message
  118. System.out.println("Message #" + (i + 1) + ":");
  119. System.out.println("t From: " + from);
  120. System.out.println("t Subject: " + subject);
  121. System.out.println("t Sent Date: " + sentDate);
  122. System.out.println("t Message: " + messageContent);
  123. System.out.println("t Attachments: " + attachFiles);
  124. }
  125.  
  126. // disconnect
  127. folderInbox.close(false);
  128. store.close();
  129. } catch (NoSuchProviderException ex) {
  130. System.out.println("No provider for pop3.");
  131. ex.printStackTrace();
  132. } catch (MessagingException ex) {
  133. System.out.println("Could not connect to the message store");
  134. ex.printStackTrace();
  135. } catch (IOException ex) {
  136. ex.printStackTrace();
  137. }
  138. }
  139.  
  140. /**
  141. * Runs this program with Gmail POP3 server
  142. */
  143. public static void main(String[] args) {
  144. String host = "pop.gmail.com";
  145. String port = "995";
  146. String userName = "****";
  147. String password = "****";
  148.  
  149. String saveDirectory = "D:/Attachments";
  150.  
  151. EmailAttachmentsDownloader receiver = new EmailAttachmentsDownloader();
  152. receiver.setSaveDirectory(saveDirectory);
  153. receiver.downloadEmailAttachments(host, port, userName, password);
  154.  
  155. }
Add Comment
Please, Sign In to add comment