查看隔離郵件後,請使用 Mail Admin Quarantine API 記錄分類決策:
- 釋出郵件,將郵件傳送給收件者。如果是內送郵件,Gmail 會將郵件傳送至收件者的信箱。如果是外寄郵件,Gmail 會傳送郵件。
- 拒絕郵件。Gmail 會捨棄郵件,不予傳送。
本頁說明如何放行或拒絕單一郵件。如要一次處理多封郵件,請參閱「批次放行或拒絕郵件」。
必要條件
- 您已完成「設定 API」中的步驟。
- 模擬具備管理員隔離權限的管理員,且包含
https://www.googleapis.com/auth/gmail.admin.quarantine範圍的存取權杖。唯讀範圍不夠。請參閱「使用全網域委派功能進行授權」。 - 處於
PENDING_TRIAGE狀態的隔離郵件資源名稱,可透過列出及搜尋隔離郵件取得。
釋出郵件
如要釋出訊息,請將 POST 要求傳送至訊息資源名稱上的 customers.quarantinedMessages 資源 :release 自訂方法,並附上空白 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"
}
}
拒絕郵件
如要拒絕訊息,請將 POST 要求傳送至訊息資源名稱上 customers.quarantinedMessages 資源的 :deny 自訂方法,並附上空白 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 用量配額」。 |