Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. public class checkMail {
  2. public static void main(String[] args) {
  3.  
  4. String host = "pop.gmail.com";// change accordingly
  5. String mailStoreType = "pop3";
  6. String username = "yourmailid@gmail.com"; // change accordingly
  7. String password = "****";// change accordingly
  8.  
  9. check(host, mailStoreType, username, password);
  10. }
  11.  
  12. private static void check(String host, String mailStoreType,String username, String password) {
  13.  
  14. try {
  15. //create properties field
  16. Properties properties = new Properties();
  17. properties.put("mail.pop3.host", host);
  18. properties.put("mail.pop3.port", "995");
  19. properties.put("mail.pop3.starttls.enable", "true");
  20.  
  21. // Get a Session
  22. Session emailSession = Session.getDefaultInstance(properties);
  23.  
  24.  
  25. //create the POP3 store object and connect with the pop server
  26. Store store = emailSession.getStore("pop3s");
  27. store.connect(host,username,password);
  28.  
  29. //create the folder object and open it
  30. Folder emailFolder = store.getFolder("INBOX");
  31. emailFolder.open(Folder.READ_ONLY);
  32.  
  33. // retrieve the messages from the folder in an array and print it
  34. Message[] messages = emailFolder.getMessages();
  35. System.out.println("messages.length---" + messages.length);
  36.  
  37. for (int i = 0, n = messages.length; i < n; i++) {
  38. Message message = messages[i];
  39. System.out.println("---------------------------------");
  40. System.out.println("Email Number " + (i + 1));
  41. System.out.println("Subject: " + message.getSubject());
  42. System.out.println("From: " + message.getFrom()[0]);
  43. System.out.println("Text: " + message.getContent().toString());
  44. /*===================== To and CC starts here ===============================*/
  45. System.out.println("n main data starts here");
  46. Address[] toList = message.getRecipients(RecipientType.TO);
  47. Address[] ccList = message.getRecipients(RecipientType.CC);
  48. System.out.println("t To: " + toList);
  49. System.out.println("t CC: " + ccList);
  50.  
  51. }
  52.  
  53. //close the store and folder objects
  54. emailFolder.close(false);
  55. store.close();
  56.  
  57. } catch (MessagingException e) {
  58. e.printStackTrace();
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62.  
  63. }
  64.  
  65. Errors:
  66. javax.mail.MessagingException: Connect failed;
  67. nested exception is:
  68. java.net.ConnectException: Connection refused: connect
  69. at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209)
  70. at javax.mail.Service.connect(Service.java:295)
  71. at javax.mail.Service.connect(Service.java:177)
  72. at servion.checkMail.check(checkMail.java:44)
  73. at servion.checkMail.main(checkMail.java:24)
  74. Caused by: java.net.ConnectException: Connection refused: connect
  75. at java.net.PlainSocketImpl.socketConnect(Native Method)
  76. at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
  77. at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
  78. at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
  79. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
  80. at java.net.Socket.connect(Socket.java:525)
  81. at java.net.Socket.connect(Socket.java:475)
  82. at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)
  83. at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
  84. at com.sun.mail.pop3.Protocol.<init>(Protocol.java:112)
  85. at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:260)
  86. at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:205)
  87. at javax.mail.Service.connect(Service.java:295)
  88. at javax.mail.Service.connect(Service.java:176)
  89. at servion.checkMail.check(checkMail.java:41)
  90. at servion.checkMail.main(checkMail.java:23)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement