如何使用 java 和 google gmail-api 获取收到的邮件?

How to get incoming mail using java and google gmail-api?

我想将 Gmail 集成到 Spring 引导项目中。我的要求是,

  1. 使用 google 授权获取用户 gmail 访问权限
  2. 发送电子邮件
  3. 保存所有集成帐户的所有新收到电子邮件
  4. 使用特定 email/domain
  5. 过滤电子邮件
  6. 显示所有电子邮件

我关注了这个文档Java Quickstart

使用这个我可以访问用户 gmail 帐户并发送电子邮件,但找不到任何关于如何使用 gmail-api 或任何其他库接收收到的邮件、过滤和处理数据的信息。

任何人都可以帮助我了解如何侦听用户新收到的电子邮件并对其进行过滤吗?

提前致谢:)

回答

首先,为了处理您收到的电子邮件,请使用 Filters. By doing so you can label all your emails "before they hit your inbox" as let's say process label and remove the INBOX label which is by default all your emails label, take a look into this JAVA Example. Once you've done this simply list 所有标记为 process 的电子邮件,使用其 labelID.

其次,使用 Gmail Push Notifications 并处理所有标记为 process 的电子邮件。

请记住,使用 Pub/Sub 需要一些额外的步骤 here

参考资料