启用 IMAP 扩展

IMAP 扩展支持

Aspose.Email API 支持一系列 IMAP 协议扩展。这些扩展使得高级操作成为可能,例如识别客户端应用程序和检索详细的邮箱元数据。虽然并非所有邮件服务器都支持这些功能,但许多流行的服务如 Gmail 支持。

本文演示了如何在 Aspose.Email 中使用以下 IMAP 扩展: ImapClient Aspose.Email 中的类:

  • IMAP ID 命令

  • IMAP LIST-EXTENDED 命令

使用 IMAP ID 命令

此命令提供有关服务器是否支持 IMAP ID 扩展的反馈,并返回来自服务器的结构化标识细节。这对日志记录、诊断以及服务器行为定制非常有用。

以下代码示例演示了如何使用 Aspose.Email 库与 IMAP 服务器交互,特别是使用 ID 命令检索服务器标识信息:

import aspose.email as ae

with ae.clients.imap.ImapClient("imap.gmail.com", 993, "username", "password") as client:

    # Set security options
    client.security_options = ae.clients.SecurityOptions.AUTO

    # Check if ID command is supported
    print(client.id_supported)

    # Send identification info to the server
    server_identification_info1 = client.introduce_client()
    server_identification_info2 = client.introduce_client(ae.clients.imap.ImapIdentificationInfo.default_value)

    # Display server response
    print(server_identification_info1)
    print(server_identification_info2)
    print(server_identification_info1.name)
    print(server_identification_info1.vendor)
    print(server_identification_info1.support_url)
    print(server_identification_info1.version)

使用 IMAP LIST-EXTENDED 命令

IMAP LIST-EXTENDED 命令(在 RFC 5258 中定义)允许客户端检索详细的文件夹层次结构和元数据,例如文件夹是否有子文件夹。这对于管理复杂邮箱结构的客户端尤其有用。

以下代码示例演示了如何使用扩展的 LIST 命令列出 Gmail 中的文件夹,并验证哪些文件夹包含子文件夹:

import aspose.email as ae

with ae.clients.imap.ImapClient("imap.gmail.com", 993, "username", "password") as client:
    folder_info_col = client.list_folders("*")
    print("Extended List Supported:", client.extended_list_supported)

    for folder_info in folder_info_col:
        folder_name = folder_info.name

        if folder_name == "[Gmail]/All Mail":
            print("Has Children:", folder_info.has_children)
        elif folder_name == "[Gmail]/Bin":
            print("Bin has children?", folder_info.has_children)
        elif folder_name == "[Gmail]/Drafts":
            print("Drafts has children?", folder_info.has_children)
        elif folder_name == "[Gmail]/Important":
            print("Important has Children?", folder_info.has_children)
        elif folder_name == "[Gmail]/Sent Mail":
            print("Sent Mail has Children?", folder_info.has_children)
        elif folder_name == "[Gmail]/Spam":
            print("Spam has Children?", folder_info.has_children)
        elif folder_name == "[Gmail]/Starred":
            print("Starred has Children?", folder_info.has_children)