Guest User

Untitled

a guest
Oct 24th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Properties;
  3.  
  4. import javax.mail.FetchProfile;
  5. import javax.mail.Folder;
  6. import javax.mail.Message;
  7. import javax.mail.MessagingException;
  8. import javax.mail.NoSuchProviderException;
  9. import javax.mail.Session;
  10. import javax.mail.Store;
  11.  
  12. public class MailReader {
  13.  
  14. public static void main(String[] args) {
  15.  
  16. String host = "pop.gmail.com";// change accordingly
  17. String mailStoreType = "pop3";
  18. String username = "";// change accordingly
  19. String password = "";// change accordingly
  20.  
  21. check(host, mailStoreType, username, password);
  22.  
  23. }
  24.  
  25. public static void check(String host, String storeType, String user, String password) {
  26. try {
  27.  
  28. // create properties field
  29. Properties properties = new Properties();
  30.  
  31. properties.put("mail.pop3.host", host);
  32. properties.put("mail.pop3.port", "995");
  33. properties.put("mail.pop3.starttls.enable", "true");
  34. Session emailSession = Session.getDefaultInstance(properties);
  35.  
  36. // create the POP3 store object and connect with the pop server
  37. Store store = emailSession.getStore("pop3s");
  38.  
  39. store.connect(host, 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. // retrieve the messages from the folder in an array and print it
  46. //Message[] messages = emailFolder.getMessages();
  47.  
  48. Message[] messages = emailFolder.getMessages();
  49. FetchProfile fp = new FetchProfile();
  50. fp.add(FetchProfile.Item.ENVELOPE);
  51. emailFolder.fetch(messages, fp);
  52. //messages = emailFolder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
  53. // Sort messages from recent to oldest
  54. Arrays.sort( messages, ( m1, m2 ) -> {
  55. try {
  56. return m2.getSentDate().compareTo( m1.getSentDate() );
  57. } catch ( MessagingException e ) {
  58. throw new RuntimeException( e );
  59. }
  60. } );
  61.  
  62. for ( Message message : messages ) {
  63. System.out.println(
  64. "sendDate: " + message.getSentDate()
  65. + " subject:" + message.getSubject() );
  66. }
  67. /*for (int i = 0, n = messages.length; i < n; i++) {
  68. Message message = messages[i];
  69. System.out.println("---------------------------------");
  70. System.out.println("Email Number " + (i + 1));
  71. System.out.println("Subject: " + message.getSubject());
  72. System.out.println("From: " + message.getFrom()[0]);
  73. System.out.println("Text: " + message.getContent().toString());
  74. }*/
  75.  
  76. // close the store and folder objects
  77. emailFolder.close(false);
  78. store.close();
  79.  
  80. } catch (NoSuchProviderException e) {
  81. e.printStackTrace();
  82. } catch (MessagingException e) {
  83. e.printStackTrace();
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
Add Comment
Please, Sign In to add comment