Guest User

Untitled

a guest
Dec 12th, 2017
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 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 zero;
  7.  
  8. import java.util.Properties;
  9.  
  10. import javax.mail.Folder;
  11. import javax.mail.Message;
  12. import javax.mail.MessagingException;
  13. import javax.mail.NoSuchProviderException;
  14. import javax.mail.Session;
  15. import javax.mail.Store;
  16. /**
  17. *
  18. * @author Nur
  19. */
  20. public class CheckingMails {
  21.  
  22.  
  23. public static void check(String host, String storeType, String user,
  24. String password)
  25. {
  26. try {
  27.  
  28.  
  29.  
  30. //create properties field
  31. Properties properties = new Properties();
  32.  
  33. properties.put("mail.pop3.host", host);
  34. properties.put("mail.pop3.port", "995");
  35. properties.put("mail.pop3.starttls.enable", "true");
  36. properties.put( "mail.pop3s.ssl.trust", host);
  37. Session emailSession = Session.getDefaultInstance(properties);
  38.  
  39. //create the POP3 store object and connect with the pop server
  40. Store store = emailSession.getStore("pop3s");
  41.  
  42. store.connect(host, user, password);
  43.  
  44. //create the folder object and open it
  45. Folder emailFolder = store.getFolder("INBOX");
  46. emailFolder.open(Folder.READ_ONLY);
  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, n = 10; i < n; i++) {
  53. Message message = messages[i];
  54. System.out.println("---------------------------------");
  55. System.out.println("Email Number " + (i + 1));
  56. System.out.println("Subject: " + message.getSubject());
  57. System.out.println("From: " + message.getFrom()[0]);
  58. System.out.println("Text: " + message.getContent().toString());
  59.  
  60. }
  61.  
  62. //close the store and folder objects
  63. emailFolder.close(false);
  64. store.close();
  65.  
  66. } catch (NoSuchProviderException e) {
  67. e.printStackTrace();
  68. } catch (MessagingException e) {
  69. e.printStackTrace();
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. }
  73. }
  74.  
  75. public static void main(String[] args) {
  76.  
  77. String host = "pop.gmail.com";// change accordingly
  78. String mailStoreType = "pop3";
  79. String username = "tinenourelhouda@gmail.com";// change accordingly
  80. String password = "Password";// change accordingly
  81.  
  82. check(host, mailStoreType, username, password);
  83.  
  84. }
  85.  
  86. }
Add Comment
Please, Sign In to add comment