Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Properties;
- import java.util.function.Consumer;
- import javax.mail.Authenticator;
- import javax.mail.Folder;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.NoSuchProviderException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Store;
- import javax.mail.search.SearchTerm;
- public class EmailTestMain {
- public static void main(String[] args) {
- String username = "[email protected]";
- String password = "Password";
- read(username, password, message -> {
- try {
- System.out.println(message.getSubject());
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- }, Folder.READ_WRITE, null);
- }
- public static void read(final String username, final String password, Consumer<Message> messageConsumer,
- int folderAction, SearchTerm searchTerm) {
- final String host = "secureimap.t-online.de";
- Properties props = new Properties();
- props.setProperty("mail.imap.host", host);
- props.setProperty("mail.imap.user", username);
- props.setProperty("mail.imap.ssl.enable", "true");
- props.setProperty("mail.imap.ssl.tr ust", host); // JAVA truststore
- props.setProperty("mail.imap.socketFactory", "993");
- props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- props.setProperty("mail.imap.socketFactory.fallback", "false");
- props.setProperty("mail.imap.port", "993");
- // Start SSL connection
- Session session = Session.getDefaultInstance(props, new Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- try (Store store = session.getStore("imap")) {
- store.connect(host, username, password);
- try (Folder emailFolder = store.getFolder("INBOX")) {
- emailFolder.open(folderAction);
- if (searchTerm != null) {
- Arrays.stream(emailFolder.search(searchTerm)).forEach(messageConsumer::accept);
- } else {
- Arrays.stream(emailFolder.getMessages()).forEach(messageConsumer::accept);
- }
- }
- } catch (NoSuchProviderException e) {
- e.printStackTrace();
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- }
- }
- com.sun.mail.util.MailConnectException: Couldn't connect to host, port: secureimap.t-online.de, 993; timeout -1;
- nested exception is:
- java.net.ConnectException: Connection refused: connect
- at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:740)
- at javax.mail.Service.connect(Service.java:366)
- at javax.mail.Service.connect(Service.java:246)
- at de.bamberg.jugger.mail.EmailTestMain.read(EmailTestMain.java:55)
- at de.bamberg.jugger.mail.EmailTestMain.main(EmailTestMain.java:21)
- Caused by: java.net.ConnectException: Connection refused: connect
- at java.net.DualStackPlainSocketImpl.connect0(Native Method)
- at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
- at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
- at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
- at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
- at java.net.PlainSocketImpl.connect(Unknown Source)
- at java.net.SocksSocketImpl.connect(Unknown Source)
- at java.net.Socket.connect(Unknown Source)
- at java.net.Socket.connect(Unknown Source)
- at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:353)
- at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:218)
- at com.sun.mail.iap.Protocol.<init>(Protocol.java:134)
- at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:131)
- at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:763)
- at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:698)
- ... 4 more
Advertisement
Add Comment
Please, Sign In to add comment