Advertisement
Guest User

Untitled

a guest
Feb 4th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import java.util.Properties;
  2.  
  3. import javax.mail.Folder;
  4. import javax.mail.Message;
  5. import javax.mail.MessagingException;
  6. import javax.mail.NoSuchProviderException;
  7. import javax.mail.Session;
  8. import javax.mail.Store;
  9.  
  10. public class CheckingMail {
  11.  
  12. public static void check(String host, String storeType, String user,
  13. String password)
  14. {
  15. try {
  16.  
  17. //create properties field
  18. Properties properties = new Properties();
  19.  
  20. properties.put("mail.pop3.host", host);
  21. properties.put("mail.pop3.port", "995");
  22. properties.put("mail.pop3.starttls.enable", "true");
  23. Session emailSession = Session.getDefaultInstance(properties);
  24.  
  25. //create the POP3 store object and connect with the pop server
  26. Store store = emailSession.getStore("pop3s");
  27.  
  28. store.connect(host, user, password);
  29.  
  30. //create the folder object and open it
  31. Folder emailFolder = store.getFolder("INBOX");
  32. emailFolder.open(Folder.READ_ONLY);
  33.  
  34. // retrieve the messages from the folder in an array and print it
  35. Message[] messages = emailFolder.getMessages();
  36. System.out.println("messages.length---" + messages.length);
  37.  
  38. for (int i = 0, n = messages.length; i < n; i++) {
  39. Message message = messages[i];
  40. System.out.println("---------------------------------");
  41. System.out.println("Email Number " + (i + 1));
  42. System.out.println("Subject: " + message.getSubject());
  43. System.out.println("From: " + message.getFrom()[0]);
  44. System.out.println("Text: " + message.getContent().toString());
  45.  
  46. }
  47.  
  48. //close the store and folder objects
  49. emailFolder.close(false);
  50. store.close();
  51.  
  52. } catch (NoSuchProviderException e) {
  53. e.printStackTrace();
  54. } catch (MessagingException e) {
  55. e.printStackTrace();
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. }
  60.  
  61. public static void main(String[] args) {
  62.  
  63. String host = "pop.gmail.com";// change accordingly
  64. String mailStoreType = "pop3";
  65. String username = "yourmail@gmail.com";// change accordingly
  66. String password = "*****";// change accordingly
  67.  
  68. check(host, mailStoreType, username, password);
  69.  
  70. }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement