Java语言实现监听QQ邮箱
立即下载
资源介绍:
基于java的开源库,实现对QQ邮箱的监听。依赖jakarta.mail-api和angus-mail。
package com.monitorqqmaildemo;
/*
* Copyright (c) 1996, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
import jakarta.mail.Authenticator;
import jakarta.mail.Folder;
import jakarta.mail.FolderClosedException;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.Store;
import jakarta.mail.event.MessageCountAdapter;
import jakarta.mail.event.MessageCountEvent;
import java.util.Properties;
import org.eclipse.angus.mail.imap.IMAPFolder;
/**
* 监听QQ邮箱的示例代码
* @author szuli
*/
public class MonitorQQMailDemo {
public static void main(String argv[]) {
Properties props = new Properties();
//指定邮箱服务器地址
props.setProperty("mail.imap.host", "imap.qq.com");
//指定邮箱服务端口
props.setProperty("mail.imap.port", "993");
//支持ssl
props.setProperty("mail.imap.ssl.enable", "true");
//支持验证
props.setProperty("mail.imap.auth", "true");
//邮箱用户(注意:需要修改为正确的)
props.setProperty("mail.imap.user", "******@qq.com");
//邮箱密码或授权码(注意:需要修改为正确的)
props.setProperty("mail.imap.pass", "ufykddhsmnzsbhhd");
//调试开关
props.setProperty("mail.debug", "true");
try {
// 创建Authenticator对象,下面建立Session时,需要使用。
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
if (Boolean.valueOf(props.getProperty("mail.smtp.auth"))) {
return new PasswordAuthentication(props.getProperty("mail.smtp.user"), props.getProperty("mail.smtp.pass"));
}
return super.getPasswordAuthentication();
}
};
//创建Session(要加Authenticator,否则idle不去作用,接收不到邮件目录更新事件)
Session session = Session.getInstance(props, auth);
//获取 a Store object
Store store = session.getStore("imap");
//建立链接
store.connect(props.getProperty("mail.imap.host"), props.getProperty("mail.imap.user"), props.getProperty("mail.imap.pass"));
//打开收件箱
Folder inBox = store.getFolder("INBOX");
if (inBox == null || !inBox.exists()) {
System.out.println("Invalid folder");
System.exit(1);
}
//更加需要设置收件箱为只读或者读写。
inBox.open(Folder.READ_WRITE);
System.out.println(inBox.getName());
int msgCount = inBox.getMessageCount();
if (msgCount != 0) {
//邮件总数
System.out.println("邮件总数:" + msgCount);
// 取最新的邮件信息
Message msg = inBox.getMessage(1);
System.out.println("邮件主题:" + msg.getSubject());
System.out.println("发件人:" + msg.getFrom());
System.out.println("收件人:" + msg.getRecipients(Message.RecipientType.TO));
System.out.println("抄送人:" + msg.getRecipients(Message.RecipientType.CC));
System.out.println("密送人:" + msg.getRecipients(Message.RecipientType.BCC));
System.out.println("邮件内容:" + msg.getContent());
}
// 添加监听器,监听收件箱(当收件箱收到新邮件时,监听器将被触发)
inBox.addMessageCountListener(new MessageCountAdapter() {
//收件箱收到新邮件时,这个方法将被触发
public void messagesAdded(MessageCountEvent messageCountEvent) {
//输出邮件
Message[] msgs = messageCountEvent.getMessages();
System.out.println("Got " + msgs.length + " new messages");
// Just dump out the new messages
for (int i = 0; i < msgs.length; i++) {
try {
System.out.println("-----");
System.out.println("Message "
+ msgs[i].getMessageNumber() + ":");
System.out.println(msgs[i].getSubject());
} catch (Exception ioex) {
ioex.printStackTrace();
}
}
}
});
//是否支持IDLE机制(QQ邮箱支持)
boolean supportsIdle = false;
try {
if (inBox instanceof IMAPFolder) {
IMAPFolder f = (IMAPFolder) inBox;
//启动监听(当前线程在这里等待,直到有新邮件)
f.idle();
supportsIdle = true;
}
} catch (FolderClosedException fex) {
throw fex;
} catch (MessagingException mex) {
supportsIdle = false;
}
//如果支持idle,那就是使用idle监听;否则使用轮询机制。
//轮询间隔,单位为毫秒。
int freq = 1000;
for (;;) {
if (supportsIdle && inBox instanceof IMAPFolder) {
IMAPFolder f = (IMAPFolder) inBox;
//支持idle机制,等待新邮件事件。
f.idle();
System.out.println("IDLE done");
} else {
//间隔指定时间,自动查询邮件,速发事件
Thread.sleep(freq);
inBox.getMessageCount();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}