Kích hoạt Tiện Ích IMAP

Hỗ Trợ Tiện Ích IMAP

API Aspose.Email cung cấp hỗ trợ cho một loạt các tiện ích giao thức IMAP. Các tiện ích này cho phép các thao tác nâng cao như xác định ứng dụng khách và lấy siêu dữ liệu chi tiết của hộp thư. Mặc dù không phải tất cả các máy chủ email đều hỗ trợ các tính năng này, nhưng nhiều dịch vụ phổ biến như Gmail vẫn hỗ trợ.

Bài viết này minh họa cách sử dụng các tiện ích IMAP sau với ImapClient lớp trong Aspose.Email:

  • Lệnh IMAP ID

  • Lệnh IMAP LIST-EXTENDED

Sử Dụng Lệnh IMAP ID

Lệnh này cung cấp phản hồi về việc máy chủ có hỗ trợ tiện ích IMAP ID hay không và trả về chi tiết định danh có cấu trúc từ máy chủ. Điều này có thể hữu ích cho việc ghi log, chẩn đoán và tùy chỉnh hành vi của máy chủ.

Đoạn mã mẫu dưới đây minh họa cách sử dụng thư viện Aspose.Email để tương tác với máy chủ IMAP, cụ thể là để lấy thông tin định danh máy chủ bằng lệnh 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)

Sử Dụng Lệnh IMAP LIST-EXTENDED

Lệnh IMAP LIST-EXTENDED (được định nghĩa trong RFC 5258) cho phép khách hàng lấy chi tiết cấu trúc thư mục và siêu dữ liệu, chẳng hạn như liệu thư mục có thư mục con hay không. Điều này đặc biệt hữu ích cho các khách hàng quản lý cấu trúc hộp thư phức tạp.

Đoạn mã mẫu dưới đây minh họa cách liệt kê các thư mục trong Gmail bằng lệnh LIST mở rộng và xác minh thư mục nào có thư mục con:

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)