Guest User

Untitled

a guest
Aug 30th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. private static void receiveEmail(String host, String mailStoreType, String
  2. username, String password) {
  3. try {
  4. Properties properties = new Properties();
  5. properties.put("mail.pop3.host", host);
  6. Session emailSession = Session.getDefaultInstance(properties);
  7.  
  8.  
  9.  
  10. POP3Store emailStore = (POP3Store) emailSession.getStore(mailStoreType);
  11. emailStore.connect(username, password);
  12.  
  13. //3) create the folder object and open it
  14. Folder emailFolder = emailStore.getFolder("INBOX");
  15. emailFolder.open(Folder.READ_ONLY);
  16. Message[] messages = emailFolder.getMessages();
  17. for (int i = 0; i < messages.length; i++) {
  18. Message message = messages[i];
  19. System.out.println("---------------------------------");
  20. System.out.println("Email Number " + (i + 1));
  21. System.out.println("Subject: " + message.getSubject());
  22. System.out.println("From: " + message.getFrom()[0]);
  23. System.out.println("Text: " + message.getContent().toString());
  24. }
  25. emailFolder.close(false);
  26. emailStore.close();
  27.  
  28. } catch (NoSuchProviderException e) {e.printStackTrace();}
  29. catch (MessagingException e) {e.printStackTrace();}
  30. catch (IOException e) {e.printStackTrace();}
  31. }
  32. public static void main(String[] args) {
  33. String host = "malikamar.com";
  34. String mailStoreType = "pop3";
  35. String username= "user2@malikamar.com";
  36. String password= "user2";
  37. receiveEmail(host, mailStoreType, username, password);
  38. }
Add Comment
Please, Sign In to add comment