admin管理员组

文章数量:1405124

I am trying to create an Application that reads my Emails, and I read about the IdleManager. My understanding is, that you just add the Folder you want to watch over, create an MessageCountListener and thats it.

Apart from adding the Properties "mail.event.scope" and "mail.event.executor"

Am I doing something wrong or is it just not working?

private Session session;
private Store store;
private IMAPFolder inboxFolder;

private ExecutorService es;

private JakartaGmailReader() {
    this.es = Executors.newCachedThreadPool();
    try {
        this.password = Files.readString(Path.of(this.getClass()
                .getResource("/AppPasswortGmail.txt")
                .toURI()));
        this.createSession();
        this.createStore();
        this.inboxFolder = this.getFolderFromStore("INBOX");
        this.inboxFolder.addMessageChangedListener(e -> {
            System.out.println("MessageChange Listener");
            System.out.println(e);
        });
        this.inboxFolder.addMessageCountListener(new MessageCountListener() {

            @Override
            public void messagesRemoved(MessageCountEvent e) {
                System.out.println("Removed");
                System.out.println(e);
            }

            @Override
            public void messagesAdded(MessageCountEvent e) {
                System.out.println("Added");
                System.out.println(e);
            }
        });
        IdleManager idleManager = new IdleManager(this.session, this.es);
        idleManager.watch(this.inboxFolder);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

private void createSession() {
    this.session = Session.getInstance(this.getImapProperties());
}

private void createStore() throws MessagingException {
    this.store = this.session.getStore("imaps");
    this.store.connect("imap.gmail", JakartaGmailReader.EMAIL, this.password);
}

private Properties getImapProperties() {
    Properties props = new Properties();
    props.put("mail.imaps.host", "imap.gmail");
    props.put("mail.imaps.ssl.trust", "imap.gmail");
    props.put("mail.imaps.port", "993");
    props.put("mail.imaps.starttls.enable", "true");
    props.put("mail.imaps.connectiontimeout", "10000");
    props.put("mail.imaps.timeout", "10000");
    props.put("mail.imaps.usesocketchannels", "true");
    props.put("mail.event.scope", "session"); // or "application"
    props.put("mail.event.executor", this.es);
    props.put("mail.store.protocol", "imaps");
    return props;
}

private IMAPFolder getFolderFromStore(String folderName) throws MessagingException {
    IMAPFolder folder = (IMAPFolder) this.store.getFolder(folderName);
    folder.open(Folder.READ_WRITE);
    return folder;
}

本文标签: javaIs Jakarta Mail Idle Manager not workingStack Overflow