背景:公司业务需要,偶尔会有特定发件人发过来邮件,要处理邮件中指定格式文本及附件;
方案:
- addMessageCountListener Java-Mail自带监听邮件方法;
- 轮训方式查询未读数量;
问题:
- 监听:
a. 该监听方法长时间会死亡(据说会超过30分钟自动死亡),所以需要用到getMsgCount
方法触发addMessageCountListener
该方法,从而进行监听;
b. 自动死亡,处理方案2,用到idle()
,但是测试过程中,时间长了也会凉凉,收不到新邮件;
c. 方案2升级版,用到idleManager.watch(folder)
,但是测试中,时间长了也会凉凉。
d. 而且听说超过30分钟整个链接自动死亡,还需要定时发送NOOP消息到服务端;
e. 总体来说,很麻烦; - 轮训:能解决上面一切问题,但是比较傻逼,比较low,不太智能;
代码(使用到idle,noop)
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
<scope>compile</scope>
</dependency>
package cn.nocov.hospital.manage.remote;
import cn.hutool.core.thread.ThreadUtil;
import cn.nocov.hospital.common.constant.Constant;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;
import com.sun.mail.imap.IdleManager;
import java.time.LocalDateTime;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.event.MessageCountAdapter;
import javax.mail.event.MessageCountEvent;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
/**
* @author: Zek
* @date: 2022/2/11 on 9:02 AM
* @desc:
*/
@Slf4j
public class ListenMail2 {
private static final Properties PROPS = System.getProperties();
static {
PROPS.setProperty("mail.imap.auth", "true");
PROPS.setProperty("mail.store.protocol", "imap");
PROPS.setProperty("mail.imap.ssl.enable", "true");
PROPS.setProperty("mail.imap.auth.login.disable", "true");
PROPS.setProperty("mail.imap.usesocketchannels", "true");
}
private IMAPFolder folder;
private IMAPStore store;
private IdleManager idleManager;
private void openStore() throws Exception {
Session session = Session.getInstance(PROPS, null);
ExecutorService es = Executors.newCachedThreadPool();
this.idleManager = new IdleManager(session, es);
// 2、通过session得到Store对象
this.store = (IMAPStore) session.getStore("imap");
// 3、连上邮件服务器
store.connect("imap.qq.com", 993, "88888@qq.com", "8888");
}
private void openFolder() {
try {
openStore();
this.folder = (IMAPFolder) this.store.getFolder("INBOX");
if (folder == null) {
throw new Exception("No IMAP INBOX");
}
System.out.println("启动时间:" + LocalDateTime.now().format(Constant.HH_MM_SS));
this.folder.open(Folder.READ_WRITE);
this.folder.addMessageCountListener(
new MessageCountAdapter() {
@SneakyThrows
@Override
public void messagesAdded(MessageCountEvent ev) {
Message[] msgs = ev.getMessages();
for (Message msg : msgs) {
System.out.println(
LocalDateTime.now().format(Constant.HH_MM_SS) + "收到新的msg:" + msg.getSubject());
}
try {
idleManager.watch(folder);
} catch (MessagingException e) {
log.error("idleManager.watch(folder) 异常:", e);
}
}
});
idleManager.watch(folder);
// log.info("开始走判断folder.idle");
// ThreadUtil.execAsync(
// () -> {
// try {
// while (true) {
// log.info("来了idle---------------------");
// folder.idle();
// }
// } catch (MessagingException e) {
// e.printStackTrace();
// }
// });
// 中断后继续idle
// for (; ; ) {
// try {
// log.info("开始走判断folder.idle,进来了1");
// if (!folder.isOpen()) {
// log.warn("idle -------------------- 重新打开folder");
// folder.open(Folder.READ_WRITE);
// }
// log.info("开始走判断folder.idle,进来了2");
// if (folder instanceof IMAPFolder) {
// IMAPFolder f = (IMAPFolder) folder;
// log.info("开始走判断folder.idle,进来了2.1,支持idle");
// f.idle();
// } else {
// log.info("开始走判断folder.idle,进来了2.2,不支持idle");
// folder.getMessageCount();
// }
// log.info("开始走判断folder.idle,进来了3");
// log.info("idle -------------------- 邮件活动。。。");
// } catch (FolderClosedException e) {
// log.error("异常:FolderClosedException,", e);
// }
// }
} catch (Exception e) {
log.error("发生异常:", e);
}
}
private void worker() {
try {
while (true) {
ThreadUtil.sleep(5000);
System.out.println("为了让他执行:" + this.folder.getMessageCount());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void noop() {
ThreadUtil.sleep(50000);
log.info("定时通信,保持邮箱连接有效,time:{}", LocalDateTime.now());
if (folder == null) {
log.info("初始化mail 连接");
openFolder();
}
log.info("发送NOOP 命令");
try {
Object val =
folder.doCommand(
p -> {
p.simpleCommand("NOOP", null);
return null;
});
} catch (MessagingException e) {
log.error("NOOP 异常", e);
}
log.info("发送NOOP 命令 done!");
}
public static void main(String[] args) {
ListenMail2 mail2 = new ListenMail2();
mail2.openFolder();
// mail2.noop();
// ThreadUtil.execAsync(mail2::worker);
}
}