public static void main(String[] args) {
/* String path="/fundDeposits/eDeposits/nominalAcccount";
System.out.println(request(path,null));*/
// 邮件服务器配置
String host = "imap.exmail.qq.com"; // 替换为你的IMAP服务器地址
String username = "xxx@meiyibao.com"; // 替换为你的邮箱地址
String password = "xxxx"; // 替换为你的邮箱密码
// 设置属性
Properties properties = new Properties();
properties.put("mail.store.protocol", "imap");
properties.put("mail.imap.auth", "true");
properties.put("mail.imap.host", host);
properties.put("mail.imap.port", "993"); // IMAP SSL端口
properties.put("mail.imap.ssl.enable", "true");
properties.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.imap.socketFactory.fallback", "false");
properties.put("mail.imap.fetchsize", "1048576");
try {
// 创建会话
Session session = Session.getDefaultInstance(properties);
// 连接到邮件存储
Store store = session.getStore("imap");
store.connect(username, password);
// 打开收件箱
Folder defaultFolder = store.getDefaultFolder();
Folder[] folders = defaultFolder.list("*");
for (Folder folder : folders) {
System.out.println("文件夹名称: " + folder.getName());
System.out.println("完整路径: " + folder.getFullName());
System.out.println("分隔符: " + folder.getSeparator());
System.out.println("类型: " + (folder.getType() == Folder.HOLDS_MESSAGES ? "包含邮件" : "包含子文件夹"));
System.out.println("-------------------------");
}
// 2. 读取收件箱内容(延续之前的功能)
System.out.println("
===== 收件箱邮件内容 =====");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
Message[] messages = inbox.getMessages();
inbox.fetch(messages, fp);
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
System.out.println("邮件 #" + (i + 1));
System.out.println("主题: " + message.getSubject());
System.out.println("发件人: " + InternetAddress.toString(message.getFrom()));
System.out.println("日期: " + message.getSentDate());
// 获取邮件内容
Object content = message.getContent();
if (content instanceof String) {
System.out.println("内容: " + content);
} else if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
for (int j = 0; j < multipart.getCount(); j++) {
BodyPart bodyPart = multipart.getBodyPart(j);
String disposition = bodyPart.getDisposition();
if (disposition != null && disposition.equals(Part.ATTACHMENT)) {
System.out.println("附件: " + bodyPart.getFileName());
} else {
System.out.println("内容: " + bodyPart.getContent());
}
}
}
System.out.println("-------------------------");
}
// 关闭连接
inbox.close(false);
store.close();
} catch (Exception e) {
e.printStackTrace();
}
}
以上代码测试过了,没有问题。
需要注意的是,
1.腾讯企业邮箱设置->收发信设置->开启服务->开启IMAP/SMTP服务或者开启POP/SMTP服务,勾选
2.收取选项中需要根据自身需求,勾选收取“我的文件夹” (对 POP3、IMAP、Exchange 协议有效)和收取 全部最近2年最近1年最近3个月最近30天 的邮件
© 版权声明
文章版权归作者所有,未经允许请勿转载。如内容涉嫌侵权,请在本页底部进入<联系我们>进行举报投诉!
THE END
暂无评论内容