日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

API開放平臺場景示例:列出郵件和附件并下載附件

調用“批量獲取指定文件夾下的郵件列表”接口,獲取郵件基本信息,選取郵件ID,傳入“列出郵件的全部附件”接口,獲取附件ID和名稱,傳入“創建下載會話(附件)并下載附件”接口并調用,完成下載。

相關接口

批量獲取指定文件夾下的郵件列表

列出郵件的全部附件

創建下載會話(附件)并下載附件

基本流程

image

Python示例代碼

API開放平臺代碼示例:獲取訪問憑證

重要

風險提示:下述代碼在Python 3.11.9進行的測試,用于生產環境之前請務必先做好測試。

# -*- coding: utf-8 -*-
import requests
# 導入獲取訪問令牌的函數,路徑根據實際情況進行修改,或直接給access_token賦值用于測試
from api_demo.get_access_token import access_token


def list_mails(email_account, cursor, folder_id):
    """
        批量獲取指定文件夾下的郵件列表,每次最多獲取100封郵件
        文檔:https://mailbestwisewords.com/openapi/index.html#/operations/alimailpb_alimail_mailagent_open_MailService_ListMessages
        參數:
        email_account -- 郵箱賬號
        cursor -- 游標,用于分頁獲取數據
        folder_id -- 郵件文件夾ID

        返回:
        郵件列表的JSON響應
        """
    print('接口名稱:,批量獲取指定文件夾下的郵件列表,每次最多獲取100封郵件')
    url = f"https://alimail-cn.aliyuncs.com/v2/users/{email_account}/mailFolders/{folder_id}/messages"

    querystring = {"cursor": cursor, "size": "100", "orderby": "DES"}

    headers = {'Content-Type': 'application/json', 'Authorization': 'bearer ' + access_token}

    response = requests.request("GET", url, headers=headers, params=querystring)

    print(response.text.encode('GBK', 'ignore'))
    return response.json()


def list_mails_attachments(v_email, v_id):
    """
        列出郵件的全部附件
        接口名稱:,列出郵件的全部附件
        文檔:https://mailbestwisewords.com/openapi/index.html#/operations/alimailpb_alimail_mailagent_open_MailService_ListAttachments
        參數:
        v_email -- 郵箱賬號
        v_id -- 郵件ID

        返回:
        郵件附件列表的JSON響應
        """

    url = f"https://alimail-cn.aliyuncs.com/v2/users/{v_email}/messages/{v_id}/attachments"

    querystring = {}

    headers = {'Content-Type': 'application/json', 'Authorization': 'bearer ' + access_token}

    response = requests.request("GET", url, headers=headers, params=querystring)

    print('##########################################################################')
    print('請求參數:', querystring)
    print('返回參數:', response.status_code, response.text)
    print('##########################################################################')
    return response.json()['attachments']


def download_mails_attachments(v_email, v_id, attachment_id, attachment_name):
    """
        創建下載會話(附件)并下載附件
        接口名稱:創建下載會話(附件)
        文檔:https://mailbestwisewords.com/openapi/index.html#/operations/alimailpb_alimail_mailagent_open_MailService_CreateAttachmentDownloadSession
        參數:
        v_email -- 郵箱賬號
        v_id -- 郵件ID
        attachment_id -- 附件ID
        attachment_name -- 附件名稱,用于保存附件
        """

    url = f"https://alimail-cn.aliyuncs.com/v2/users/{v_email}/messages/{v_id}/attachments/{attachment_id}/$value"

    headers = {
        "Content-Type": "application/json",
        "Authorization": 'bearer ' + access_token
    }

    response = requests.request("GET", url, headers=headers)
    with open(attachment_name, 'wb') as f:
        f.write(response.content)


def get_all_mails(email_account, v_folder_id):
    """
        獲取郵件基本信息

        參數:
        email_account -- 郵箱賬號
        v_folder_id -- 郵件文件夾ID

        返回:
        郵件信息列表
    """
    records = []
    cursor = ""
    while True:
        # 獲取數據
        parsed_data = list_mails(email_account, cursor, v_folder_id)

        # 提取所需字段
        for v_mail in parsed_data['messages']:
            record = {
                'id': v_mail['id'],
                'mailId': v_mail['mailId'],
                'sentDateTime': v_mail['sentDateTime'],  # 和北京時間時差8小時
                'hasAttachments': v_mail['hasAttachments']
            }
            records.append(record)
        # 更新游標
        cursor = parsed_data["nextCursor"]
        print(f'hasMore={parsed_data["hasMore"]},nextCursor={cursor}')
        if not parsed_data["hasMore"]:
            print(f'沒有更多數據')
            break
    return records


print('======================================')
folder_id = {
    "發件箱": "1",
    "收件箱": "2",
    "垃圾箱": "3",
    "草稿箱": "5",
    "已刪除": "6"
}

email_account = 'test@example.com'
all_data = get_all_mails(email_account, folder_id.get('收件箱'))
print(f'郵件基本信息:共{len(all_data)}封,{all_data}')
print('======================================')
# 根據郵件時間等信息選擇需要下載的附件的郵件id,如'DzzzzzzNpQ9'
email_id = 'DzzzzzzNpQ9'
list_attachments = list_mails_attachments(email_account, email_id)  # 列出郵件的全部附件,成功
print('======================================')
for v_file in list_attachments:
    print(f'附件名稱:{v_file["name"]}')
    download_mails_attachments(email_account, email_id, v_file["id"], v_file["name"])
    print('下載完成')
print('======================================')

運行結果

image