放行或拒绝邮件

查看隔离的邮件后,使用 Mail Admin Quarantine API 记录您的分类决定:

  • 释放邮件,将其递送给收件人。对于入站邮件,Gmail 会将其递送到收件人的邮箱。对于出站邮件,Gmail 会发送该邮件。
  • 拒绝邮件。Gmail 会丢弃邮件,不予递送。

本页面介绍了如何释放或拒绝单个邮件。如需一次性处理多封邮件,请参阅批量释放或拒绝邮件。

前提条件

  • 您已完成设置 API 中的步骤。
  • 一个访问令牌,用于模拟具有管理员隔离区权限的管理员,并包含 https://www.googleapis.com/auth/gmail.admin.quarantine 范围。只读范围不够。请参阅使用全网域授权进行授权。
  • 处于 PENDING_TRIAGE 状态的隔离邮件的资源名称,您可以通过列出和搜索隔离邮件获取。

释放邮件

如需释放消息,请向消息资源名称上的 customers.quarantinedMessages 资源的 :release 自定义方法发送 POST 请求,并附带空的 JSON 正文:

POST https://mailadminquarantine.googleapis.com/v1/customers/CUSTOMER_ID/quarantinedMessages/MESSAGE_OWNER:MESSAGE_ID:release
Authorization: Bearer ACCESS_TOKEN
Content-Type: application/json

{}

响应在 message 字段中包含更新后的消息,其中 state 设置为 RELEASED:

{
  "message": {
    "name": "customers/01abc23de/quarantinedMessages/user@example.com:18d3ef45a8b2c1d0",
    "messageId": "18d3ef45a8b2c1d0",
    "messageOwner": "user@example.com",
    "state": "RELEASED"
  }
}

拒绝消息

如需拒绝消息,请向消息资源名称上的 customers.quarantinedMessages 资源的 :deny 自定义方法发送 POST 请求,并附带空的 JSON 正文:

POST https://mailadminquarantine.googleapis.com/v1/customers/CUSTOMER_ID/quarantinedMessages/MESSAGE_OWNER:MESSAGE_ID:deny
Authorization: Bearer ACCESS_TOKEN
Content-Type: application/json

{}

响应在 message 字段中包含更新后的消息,其中 state 设置为 DENIED:

{
  "message": {
    "name": "customers/01abc23de/quarantinedMessages/user@example.com:18d3ef45a8b2c1d0",
    "messageId": "18d3ef45a8b2c1d0",
    "messageOwner": "user@example.com",
    "state": "DENIED"
  }
}

示例:对待审批消息进行分类

以下 Python 示例会搜索待分类的邮件,检查每封邮件的主题,并根据示例规则释放或拒绝邮件。将该规则替换为您自己的分类逻辑。

import requests

BASE_URL = 'https://mailadminquarantine.googleapis.com/v1'
CUSTOMER_ID = '01abc23de'  # Your customer ID without the leading 'C'.
headers = {
    'Authorization': f'Bearer {access_token}',
    'Content-Type': 'application/json',
}

# 1. Search for messages pending triage.
search_url = f'{BASE_URL}/customers/{CUSTOMER_ID}/quarantinedMessages:search'
search_body = {'filter': 'status = PENDING_TRIAGE', 'pageSize': 10}
response = requests.post(search_url, headers=headers, json=search_body)
response.raise_for_status()

for message in response.json().get('quarantinedMessages', []):
  name = message['name']

  # 2. Get the parsed message content.
  details = requests.get(
      f'{BASE_URL}/{name}?view=MESSAGE_VIEW_DETAILS', headers=headers)
  details.raise_for_status()
  subject = details.json().get('content', {}).get('subject', '')

  # 3. Release or deny the message.
  action = 'deny' if 'malicious' in subject.lower() else 'release'
  result = requests.post(f'{BASE_URL}/{name}:{action}', headers=headers,
                         json={})
  result.raise_for_status()
  print(f"{name}: {result.json()['message']['state']}")

错误响应

HTTP 状态 原因
400 INVALID_ARGUMENT 资源名称格式不正确。
403 PERMISSION_DENIED 被模拟的管理员无权访问包含该邮件的隔离区,或者令牌缺少 https://www.googleapis.com/auth/gmail.admin.quarantine 范围。
404 NOT_FOUND 不存在具有该资源名称的隔离邮件。邮件可能已从隔离区过期。
429 RESOURCE_EXHAUSTED 您的 Google Cloud 项目超出了写入请求配额。请参阅 Mail Admin Quarantine API 使用配额。

后续步骤