1,邮箱协议设置

1,邮箱的读取需要先配置邮箱协议,主要有两种,第一个是pop3协议,第二个是imap协议,两者之间的区别在于imap是可以区分邮件是否已读取,而pop可以通过SearchTerm查询条件过滤邮件,关于两者的配置QQ邮箱可以直接点下方QQ官方链接设置,
https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

2,腾讯企业邮箱到不需要配置,因为他默认是已经开启了pop3、imap协议的,可以直接通过账号以及密码登录连接的,但是你也可以配置专用密码进行连接,方法如下图
1,开启前

2,开启后

2,java代码实现

1,两者之前的代码实际上都差不多,区别在于邮箱服务器的配置

imap
1,QQ邮箱服务器:imap.qq.com
2,腾讯企业邮箱服务器:imap.exmail.qq.com
pop
1,QQ邮箱服务器:pop.qq.com
2,腾讯企业邮箱服务器:pop.exmail.qq.com
//涉及pom jar
<dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</version>
</dependency>
package com.example.demo.utils;import org.apache.commons.lang3.StringUtils;import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;/*** @author:Deng* @date: 2021-04-14 17:30* @remark:  腾讯邮件读取*/
public class EmailUtils {public static void readEmail() {Properties props = new Properties();props.put("mail.imap.host", "imap.exmail.qq.com");//QQ邮箱为:imap.qq.comprops.put("mail.imap.auth", "true");props.setProperty("mail.store.protocol", "imap");props.put("mail.imap.starttls.enable", "true");Session session = Session.getInstance(props);try {Store store = session.getStore();//企业邮箱如果开启了安全登录,密码需要使用专用密码,没有开启使用账号密码即可store.connect("imap.exmail.qq.com", "xxx@xx.com", "xxxxx");//store.connect("imap.qq.com", "xxx@xx.com", "授权码");//QQ邮箱需要设置授权码Folder folder = store.getFolder("Inbox");//邮箱打开方式folder.open(Folder.READ_WRITE);//收取未读邮件Message[] messages = folder.getMessages(folder.getMessageCount() - folder.getUnreadMessageCount() + 1, folder.getMessageCount());System.out.println("邮件总数: " + folder.getMessageCount());//解析的邮件的方法,就粘贴了网上的了,有需要的直接点击参考文档的链接parseFileMessage(messages);folder.setFlags(messages, new Flags(Flags.Flag.SEEN), true);}catch (Exception e){System.out.println("异常: " + e);}}public static void readEmailPop() throws Exception {Properties props = new Properties();props.put("mail.pop3.host", "pop.exmail.qq.com");props.put("mail.pop3.auth", "true");props.setProperty("mail.store.protocol", "pop3");props.put("mail.pop3.starttls.enable", "true");Session session = Session.getInstance(props);Store store = session.getStore("pop3");store.connect("pop.exmail.qq.com", "xxx@xxx.net", "xxxx");Folder folder = store.getFolder("Inbox");//邮箱打开方式folder.open(Folder.READ_WRITE);//昨天零点时间Calendar cal = Calendar.getInstance();cal.add(Calendar.DATE, -1);cal.set(Calendar.HOUR_OF_DAY, 0);cal.set(Calendar.MINUTE, 0);cal.set(Calendar.SECOND, 0);cal.set(Calendar.MILLISECOND, 0);Date mondayDate = cal.getTime();//昨天零点到当前时间的邮件SearchTerm comparisonTermGe = new SentDateTerm(ComparisonTerm.GE, mondayDate);SearchTerm comparisonTermLe = new SentDateTerm(ComparisonTerm.LE, new Date());SearchTerm comparisonAndTerm = new AndTerm(comparisonTermGe, comparisonTermLe);//条件查询只能通过pop协议的方式才生效Message[] messages = folder.search(comparisonAndTerm);//  Message[] messages = folder.getMessages();System.out.println("读取的邮件总数: " + messages.length);parseFileMessage(messages);// folder.setFlags(messages, new Flags(Flags.Flag.SEEN), true);System.out.println("邮件解析任务执行完毕");}public static void parseFileMessage(Message... messages) throws Exception {if (messages == null || messages.length < 1){System.out.println("没有可读取邮件");return;}// 解析读取到的邮件for (Message message : messages) {MimeMessage msg = (MimeMessage) message;System.out.println("------------------解析第" + msg.getMessageNumber() + "封邮件-------------------- ");System.out.println("主题: " + MimeUtility.decodeText(msg.getSubject()));System.out.println("发件人: " + getFrom(msg));System.out.println("收件人:" + getReceiveAddress(msg, null));System.out.println("发送时间:" + getSentDate(msg, null));System.out.println("是否已读:" + isSeen(msg));System.out.println("邮件优先级:" + getPriority(msg));System.out.println("是否需要回执:" + isReplySign(msg));System.out.println("邮件大小:" + msg.getSize() * 1024 + "kb");StringBuffer content = new StringBuffer(30);getMailTextContent(msg, content);System.out.println("邮件正文:" + (content.length() > 100 ? content.substring(0,100) + "..." : content));System.out.println();boolean isContainerAttachment = isContainAttachment(msg);System.out.println("是否包含附件:" + isContainerAttachment);if (isContainerAttachment) {saveAttachment(msg, "D:\\data\\emailFile\\", msg.getFileName()); //保存附件}System.out.println("------------------第" + msg.getMessageNumber() + "封邮件解析结束-------------------- ");}}/*** 获得邮件发件人* @param msg 邮件内容* @return 姓名 <Email地址>* @throws MessagingException* @throws UnsupportedEncodingException*/public static String getFrom(MimeMessage msg) throws MessagingException, UnsupportedEncodingException {String from = "";Address[] froms = msg.getFrom();if (froms.length < 1)throw new MessagingException("没有发件人!");InternetAddress address = (InternetAddress) froms[0];String person = address.getPersonal();if (person != null) {person = MimeUtility.decodeText(person) + " ";} else {person = "";}from = person + "<" + address.getAddress() + ">";return from;}/*** 获得邮件发送时间* @param msg 邮件内容* @return yyyy年mm月dd日 星期X HH:mm* @throws MessagingException*/public static String getSentDate(MimeMessage msg, String pattern) throws MessagingException {Date receivedDate = msg.getSentDate();if (receivedDate == null)return "";if (pattern == null || "".equals(pattern))pattern = "yyyy年MM月dd日 E HH:mm ";return new SimpleDateFormat(pattern).format(receivedDate);}/*** 判断邮件是否已读* @param msg 邮件内容* @return 如果邮件已读返回true,否则返回false* @throws MessagingException*/public static boolean isSeen(MimeMessage msg) throws MessagingException {return msg.getFlags().contains(Flags.Flag.SEEN);}/*** 判断邮件是否需要阅读回执* @param msg 邮件内容* @return 需要回执返回true,否则返回false* @throws MessagingException*/public static boolean isReplySign(MimeMessage msg) throws MessagingException {boolean replySign = false;String[] headers = msg.getHeader("Disposition-Notification-To");if (headers != null)replySign = true;return replySign;}/*** 获得邮件的优先级* @param msg 邮件内容* @return 1(High):紧急  3:普通(Normal)  5:低(Low)* @throws MessagingException*/public static String getPriority(MimeMessage msg) throws MessagingException {String priority = "普通";String[] headers = msg.getHeader("X-Priority");if (headers != null) {String headerPriority = headers[0];if (headerPriority.contains("1") || headerPriority.contains("High"))priority = "紧急";else if (headerPriority.contains("5") || headerPriority.contains("Low"))priority = "低";elsepriority = "普通";}return priority;}/*** 根据收件人类型,获取邮件收件人、抄送和密送地址。如果收件人类型为空,则获得所有的收件人* <p>Message.RecipientType.TO  收件人</p>* <p>Message.RecipientType.CC  抄送</p>* <p>Message.RecipientType.BCC 密送</p>* @param msg 邮件内容* @param type 收件人类型* @return 收件人1 <邮件地址1>, 收件人2 <邮件地址2>, ...* @throws MessagingException*/public static String getReceiveAddress(MimeMessage msg, Message.RecipientType type) throws MessagingException {StringBuilder receiveAddress = new StringBuilder();Address[] addresss;if (type == null) {addresss = msg.getAllRecipients();} else {addresss = msg.getRecipients(type);}if (addresss == null || addresss.length < 1)throw new MessagingException("没有收件人!");for (Address address : addresss) {InternetAddress internetAddress = (InternetAddress)address;receiveAddress.append(internetAddress.toUnicodeString()).append(",");}receiveAddress.deleteCharAt(receiveAddress.length()-1); //删除最后一个逗号return receiveAddress.toString();}/*** 判断邮件中是否包含附件** @return 存在附件返回true,不存在返回false*/public static boolean isContainAttachment(Part part) throws Exception {boolean flag = false;if (part.isMimeType("multipart/*")) {MimeMultipart multipart = (MimeMultipart) part.getContent();int partCount = multipart.getCount();for (int i = 0; i < partCount; i++) {BodyPart bodyPart = multipart.getBodyPart(i);String disp = bodyPart.getDisposition();if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {flag = true;} else if (bodyPart.isMimeType("multipart/*")) {flag = isContainAttachment(bodyPart);} else {String contentType = bodyPart.getContentType();if (contentType.contains("application")) {flag = true;}if (contentType.contains("name")) {flag = true;}}if (flag) break;}} else if (part.isMimeType("message/rfc822")) {flag = isContainAttachment((Part) part.getContent());}return flag;}/*** 保存文件** @param destDir  文件目录* @param fileName 文件名* @throws Exception 异常*/public static void saveAttachment(Part part, String destDir, String fileName) throws Exception {if (part.isMimeType("multipart/*")) {//复杂体邮件Multipart multipart = (Multipart) part.getContent();//复杂体邮件包含多个邮件体int partCount = multipart.getCount();for (int i = 0; i < partCount; i++) {//获得复杂体邮件中其中一个邮件体BodyPart bodyPart = multipart.getBodyPart(i);//迭代处理邮件体,直到附件为止String disp = bodyPart.getDisposition();String decodeName = decodeText(bodyPart.getFileName());decodeName = StringUtils.isEmpty(decodeName) ? fileName : decodeName;if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {saveFile(bodyPart.getInputStream(), destDir, decodeName);} else if (bodyPart.isMimeType("multipart/*")) {saveAttachment(bodyPart, destDir, fileName);} else {String contentType = bodyPart.getContentType();if (contentType.contains("name") || contentType.contains("application")) {saveFile(bodyPart.getInputStream(), destDir, decodeName);}}}} else if (part.isMimeType("message/rfc822")) {saveAttachment((Part) part.getContent(), destDir, fileName);}}/*** 获得邮件文本内容* @param part 邮件体* @param content 存储邮件文本内容的字符串* @throws MessagingException* @throws IOException*/public static void getMailTextContent(Part part, StringBuffer content) throws MessagingException, IOException {//如果是文本类型的附件,通过getContent方法可以取到文本内容,但这不是我们需要的结果,所以在这里要做判断boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;if (part.isMimeType("text/*") && !isContainTextAttach) {content.append(part.getContent().toString());} else if (part.isMimeType("message/rfc822")) {getMailTextContent((Part)part.getContent(),content);} else if (part.isMimeType("multipart/*")) {Multipart multipart = (Multipart) part.getContent();int partCount = multipart.getCount();for (int i = 0; i < partCount; i++) {BodyPart bodyPart = multipart.getBodyPart(i);getMailTextContent(bodyPart,content);}}}/*** 读取输入流中的数据保存至指定目录** @param is       输入流* @param fileName 文件名* @param destDir  文件存储目录*/private static void saveFile(InputStream is, String destDir, String fileName)throws Exception {createEmptyDirectory(destDir);BufferedInputStream bis = new BufferedInputStream(is);BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(destDir + fileName)));int len;while ((len = bis.read()) != -1) {bos.write(len);bos.flush();}bos.close();bis.close();}/*** 创建一个空目录*/public static void createEmptyDirectory(String directoryPath) {File file = new File(directoryPath);if (!file.exists()) {file.mkdirs();}}/*** 文本解码*/public static String decodeText(String encodeText) throws Exception {if (encodeText == null || "".equals(encodeText)) {return "";} else {return MimeUtility.decodeText(encodeText);}}
}

3,测试结果

imap测试结果:


pop测试结果

没限制查询条件结果

限制条件查询结果

4,存在问题

存在一个问题,就是在邮箱中已经设置只接收近30天的邮件,但是在imap方式中并没有生效,而pop方式是生效了,为什么imap的方式不生效呢?待解决

5,参考文档

[https://blog.csdn.net/qq_39006919/article/details/109115343]
https://blog.csdn.net/hj7jay/article/details/84062650

java腾讯邮箱读取邮件(包含企业邮箱)相关推荐

  1. 腾讯企业邮箱登录、网易企业邮箱、TOM企业邮箱,各大企业邮箱如何申请登录?

    企业邮箱,作为企业成员办公的重要环节,在申请时,很多企业面临选择上的疑问,而申请企业邮箱后,很多员工面临不知从哪里登陆的难题.本文笔者以腾讯企业邮箱登录.网易企业邮箱.TOM企业邮箱为例,介绍从申请到 ...

  2. 怎样在发信服务器上查网易邮件,网易企业邮箱:你知道如何查看网易企业邮箱邮件吗?...

    网易企业邮箱发送一封电子邮件,显示电子邮件已发送,可交付状态始终为"正在传递".那么,邮件是否已发送,以及如何查看邮件的状态?目前,网易企业邮箱已发送邮件的发送状态包括以下几种情况 ...

  3. 什么是联系人邮箱,如何使用企业邮箱给外国人发邮件?

    企业邮箱可以很好的管理联系人邮箱,并且有专属的海外通道向国外发送邮件:TOM 企业邮箱,智能管理功能和定制邮箱名字,对外可以树立公司的品牌形象,对内可以管理企业内部沟通.那么今天就一起来看看什么是联系 ...

  4. foxmail邮箱怎么导入邮件_163企业邮箱登录后怎么导入联系人?

    现在越来越多的企业使用企业邮箱办公,刚刚更换到TOM企业邮箱后,我们遇到的疑问是怎么导入我们公司的员工账号和联系人,经过客服的指导给大家分享下具体流程,大家要开通的不用急,还有感恩节企业邮箱3折优惠~ ...

  5. 收费企业邮箱注册,大企业邮箱品牌哪家好?如何注册公司邮箱?

    收费企业邮箱品牌注册哪家好?公司购买企业邮箱,选择一个企业邮箱的标准也是多维度的,例如系统的稳定性.安全性.功能便利性.性价比等.常见的企业邮箱包括网易企业邮箱.TOM企业邮箱.网易企业邮箱.腾讯企业 ...

  6. 企业qq邮箱,网易邮箱账号注册,企业邮箱哪个好,多少钱?

    看到有朋友问企业QQ邮箱,和网易邮箱等怎么样,费用多少钱.我们都知道,电子邮箱除网易.腾讯企业QQ,还有性价比极高的 TOM企业邮箱箱.那么这些邮箱中,哪个企业邮箱比较好呢?多少钱呢?接下来小编为大家 ...

  7. 企业邮箱如何购买?企业邮箱费用哪家更划算?

    在众多企业邮箱品牌服务商中为什么那么多公司选择了TOM企业邮箱呢,腾讯.TOM.网易邮箱,究竟哪个企业邮箱品牌更具性价比呢? 多场景邮件管理 邮件跨终端同步给企业邮件管理带来了很大便利,手机.电脑.i ...

  8. 外贸企业发开发信哪家邮箱好用?企业邮箱退信怎么办?

    看有小伙伴发帖企业邮箱退信怎么办?企业邮箱群发退信问题取决于垃圾邮件.邮箱通道.网络等原因.例如很多服务商因为价格便宜导致一些企业入坑,然后邮件有的发不出去.退信,有的只能进对方公司的垃圾邮箱. TO ...

  9. 企业网站 源码 服务邮箱:_公司企业邮箱购买,外贸企业邮箱用哪家服务好?

    企业日常办公,经常会用到各种办公软件,而企业邮箱便是最常用的产品.公司在购买企业邮箱时需要考虑哪些方面,尤其是对于外贸行业的企业邮箱,应该如何选择呢? 1. 安全保障 公司企业邮箱购买时,首先要关注的 ...

  10. 企业电子邮箱怎么写?企业邮箱登录入口是什么?

    以TOM企业邮箱为例,电子邮箱是通过互联网为用户提供信息传递,为用户提供发送和接收电子邮件服务,并拥有对邮件进行存储的功能.使用时需填写对方的电子邮箱地址,才能将信件发送给对方,而在连接网络的状态下能 ...

最新文章

  1. 面试---如何在List<Integer>中如何存放String类型的数据?
  2. Jenkins之邮件通知
  3. linux内核_Linux驱动编程的本质就是Linux内核编程
  4. Mysql实现幂等_阿里面试官:接口的幂等性怎么设计?
  5. LeetCode 372. 超级次方(快速幂)
  6. linux重启mysql不动了,[转载]LINUX启动/重启/停上MYSQL的命令
  7. Unique Binary Search Trees ll -深度优先遍历DFS
  8. 【数据结构】线性表的链式存储结构
  9. C++ --对象和类
  10. 2020年华为杯中国研究生数学建模出结果时间
  11. 学习笔记9--汽车线控系统技术
  12. VUE-地区选择器(V-Distpicker)
  13. 6 生僻字_抖音《生僻字》的字词成语解释完整版
  14. 淘客菜鸟百度贴吧怎么发帖子操作淘宝客
  15. 计算机写配器音乐谱子,《电脑音乐配器与制作》教学思路及教材编写
  16. 用Python登陆新版正方教务系统获取课程表(及RSA加密密码实现)
  17. js中关于时间格式转化,时间大小比较的方法
  18. 智能合约编写之Solidity的设计模式 | FISCO BCOS超话区块链专场(篇4)
  19. hi3798mv200引脚调试
  20. Population Vector Algorithm(PVA)

热门文章

  1. 如何实现大屏数字滚动效果
  2. CAD转换技巧:高版本CAD文件转换成低版本在线版最简单
  3. 网络基础之计算机网络参考模型(OSI参考模型与TCP/IP协议簇)
  4. springboot整合mybatis
  5. 南昌大学计算机学硕推免生,太狠了:他们从南昌大学保研到北大、清华、复旦、浙大等顶尖名校...
  6. python语言关键字是_Python 关键字
  7. 安卓基于MDNS协议的局域网内服务发现
  8. Win10任务栏显示窗口不折叠的设置方法
  9. mac实用小技巧之解压.xip文件
  10. 买了淘宝TeamViewer盗版账号才知道安全没保障,大家别再上当了