Guest User

Untitled

a guest
Jun 16th, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Properties;
  3. import java.util.function.Consumer;
  4.  
  5. import javax.mail.Authenticator;
  6. import javax.mail.Folder;
  7. import javax.mail.Message;
  8. import javax.mail.MessagingException;
  9. import javax.mail.NoSuchProviderException;
  10. import javax.mail.PasswordAuthentication;
  11. import javax.mail.Session;
  12. import javax.mail.Store;
  13. import javax.mail.search.SearchTerm;
  14.  
  15. public class EmailTestMain {
  16. public static void main(String[] args) {
  17. String username = "[email protected]";
  18. String password = "Password";
  19. read(username, password, message -> {
  20. try {
  21. System.out.println(message.getSubject());
  22. } catch (MessagingException e) {
  23.  
  24. e.printStackTrace();
  25. }
  26. }, Folder.READ_WRITE, null);
  27. }
  28.  
  29. public static void read(final String username, final String password, Consumer<Message> messageConsumer,
  30. int folderAction, SearchTerm searchTerm) {
  31. final String host = "secureimap.t-online.de";
  32. Properties props = new Properties();
  33.  
  34. props.setProperty("mail.imap.host", host);
  35. props.setProperty("mail.imap.user", username);
  36. props.setProperty("mail.imap.ssl.enable", "true");
  37. props.setProperty("mail.imap.ssl.tr ust", host); // JAVA truststore
  38.  
  39. props.setProperty("mail.imap.socketFactory", "993");
  40. props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  41. props.setProperty("mail.imap.socketFactory.fallback", "false");
  42. props.setProperty("mail.imap.port", "993");
  43.  
  44. // Start SSL connection
  45.  
  46. Session session = Session.getDefaultInstance(props, new Authenticator() {
  47. @Override
  48. protected PasswordAuthentication getPasswordAuthentication() {
  49. return new PasswordAuthentication(username, password);
  50. }
  51. });
  52. try (Store store = session.getStore("imap")) {
  53. store.connect(host, username, password);
  54. try (Folder emailFolder = store.getFolder("INBOX")) {
  55. emailFolder.open(folderAction);
  56. if (searchTerm != null) {
  57. Arrays.stream(emailFolder.search(searchTerm)).forEach(messageConsumer::accept);
  58. } else {
  59. Arrays.stream(emailFolder.getMessages()).forEach(messageConsumer::accept);
  60. }
  61. }
  62. } catch (NoSuchProviderException e) {
  63. e.printStackTrace();
  64. } catch (MessagingException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. }
  69.  
  70. com.sun.mail.util.MailConnectException: Couldn't connect to host, port: secureimap.t-online.de, 993; timeout -1;
  71. nested exception is:
  72. java.net.ConnectException: Connection refused: connect
  73. at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:740)
  74. at javax.mail.Service.connect(Service.java:366)
  75. at javax.mail.Service.connect(Service.java:246)
  76. at de.bamberg.jugger.mail.EmailTestMain.read(EmailTestMain.java:55)
  77. at de.bamberg.jugger.mail.EmailTestMain.main(EmailTestMain.java:21)
  78. Caused by: java.net.ConnectException: Connection refused: connect
  79. at java.net.DualStackPlainSocketImpl.connect0(Native Method)
  80. at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
  81. at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
  82. at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
  83. at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
  84. at java.net.PlainSocketImpl.connect(Unknown Source)
  85. at java.net.SocksSocketImpl.connect(Unknown Source)
  86. at java.net.Socket.connect(Unknown Source)
  87. at java.net.Socket.connect(Unknown Source)
  88. at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:353)
  89. at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:218)
  90. at com.sun.mail.iap.Protocol.<init>(Protocol.java:134)
  91. at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:131)
  92. at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:763)
  93. at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:698)
  94. ... 4 more
Advertisement
Add Comment
Please, Sign In to add comment