我正在尝试使用 POP3 协议收听新消息。我知道 Pop3 在文件夹打开时不允许新邮件出现在收件箱中。下面是我实现的代码:
import javax.mail.event.MessageCountAdapter;
import javax.mail.event.MessageCountEvent;
public class EmailListener extends MessageCountAdapter {
public EmailListener() {
}
public void messagesAdded(MessageCountEvent e) {
System.out.println("I");
}
public void messagesRemoved(MessageCountEvent e) {
System.out.println("J");
}
}
public class POPReceiver {
public POPReceiver() {
}
public void listen() throws Exception {
Properties properties = new Properties();
Session session = null;
POP3Store pop3Store = null;
String host = "NB-EX101.example.com";
String user = "user2";
properties.put(mail.pop3.host, host);
session = Session.getDefaultInstance(properties);
pop3Store = (POP3Store) session.getStore("pop3");
pop3Store.connect(user, "password");
Folder folder = pop3Store.getFolder("INBOX");
folder.addMessageCountListener(new EmailListener());
sendEmail();
}
public void sendEmail() {
// not added code, but the email sends
}
}
public static void main(String[] args) throws Exception {
POPReceiver i = new POPReceiver();
i.listen();
}
我正在使用 Microsoft Exchange 服务器。任何想法为什么它不听?
我看过http://www.coderanch.com/t/597347/java/java/Email-Listener但仍然不听。