当前位置:   article > 正文

Javamail收取邮件

javamail收取邮件
  1. package a;
  2. import java.util.ArrayList;
  3. import java.util.Properties;
  4. import javax.mail.Folder;
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.NoSuchProviderException;
  8. import javax.mail.Session;
  9. import javax.mail.Store;
  10. public class Main {
  11. public static ArrayList<String> check(String host, String storeType, String user, String password) {
  12. ArrayList<String> als = new ArrayList<>();
  13. try {
  14. // create properties field
  15. Properties properties = new Properties();
  16. properties.put("mail.pop3.host", host);
  17. properties.put("mail.pop3.port", "995");
  18. properties.put("mail.pop3.starttls.enable", "true");
  19. Session emailSession = Session.getDefaultInstance(properties);
  20. // create the POP3 store object and connect with the pop server
  21. Store store = emailSession.getStore("pop3s");
  22. store.connect(host, user, password);
  23. // create the folder object and open it
  24. Folder emailFolder = store.getFolder("INBOX");
  25. emailFolder.open(Folder.READ_ONLY);
  26. // retrieve the messages from the folder in an array and print it
  27. Message[] messages = emailFolder.getMessages();
  28. System.out.println("新闻数量:" + messages.length);
  29. for (Message message : messages) {
  30. String s = message.getSubject();
  31. als.add("新闻内容:" + s);
  32. }
  33. // close the store and folder objects
  34. emailFolder.close(false);
  35. store.close();
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. return als;
  40. }
  41. public static void main(String[] args) {
  42. String host = "outlook.office365.com";// change accordingly
  43. String mailStoreType = "pop3";
  44. String username = "www.oracle.com@outlook.com";// change accordingly
  45. String password = "123456";// change accordingly
  46. ArrayList<String> als = check(host, mailStoreType, username, password);
  47. for (String s : als) {
  48. System.out.println(s);
  49. }
  50. }
  51. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/95502
推荐阅读
相关标签
  

闽ICP备14008679号