Hỗ trợ Lệnh IMAP IDLE và Các Tiện ích mở rộng

Hỗ trợ Lệnh IMAP Idle

API Aspose.Email ImapClient cung cấp khả năng mở một kết nối tới máy chủ và chờ tin nhắn email tới. Điều này tránh việc phải truy vấn máy chủ liên tục để nhận email mới. startMonitoring nhận hai bộ xử lý: một được gọi khi tin nhắn đến hoặc bị xóa, bộ còn lại khi việc giám sát thất bại. Đoạn mã sau cho bạn thấy cách sử dụng lệnh IMAP IDLE.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Connect and log in to IMAP
ImapClient client = new ImapClient("imap.domain.com", "username", "password");

client.startMonitoring(new ImapMonitoringEventHandler() {
    public void invoke(Object sender, ImapMonitoringEventArgs e) {
        System.out.println(e.getNewMessages().length);
        System.out.println(e.getDeletedMessages().length);
    }
}, new ImapMonitoringErrorEventHandler() {
    public void invoke(Object sender, ImapMonitoringErrorEventArgs e) {
        System.out.println("Monitoring of " + e.getFolderName() + " failed: " + e.getError());
    }
}, "Inbox");

// ... later, stop waiting for new messages
client.stopMonitoring("Inbox");

Hỗ trợ các Tiện ích mở rộng IMAP

API Aspose.Email cung cấp hỗ trợ cho các phần mở rộng IMAP. Các mẫu dưới đây cho thấy các phần mở rộng hiện đang được API hỗ trợ. Không phải máy chủ nào cũng hỗ trợ các phần mở rộng này.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
final ImapClient client = new ImapClient("imap.gmail.com", 993, "username", "password");
try {
    // Set SecurityOptions
    client.setSecurityOptions(SecurityOptions.Auto);
    System.out.println(client.getIdSupported());

    ImapIdentificationInfo serverIdentificationInfo1 = client.introduceClient();
    ImapIdentificationInfo serverIdentificationInfo2 = client.introduceClient(ImapIdentificationInfo.getDefaultValue());

    // Display ImapIdentificationInfo properties
    System.out.println(serverIdentificationInfo1.toString() + serverIdentificationInfo2.toString());
    System.out.println(serverIdentificationInfo1.getName());
    System.out.println(serverIdentificationInfo1.getVendor());
    System.out.println(serverIdentificationInfo1.getSupportUrl());
    System.out.println(serverIdentificationInfo1.getVersion());
} finally {
    if (client != null)
        client.dispose();
}

Lệnh Danh sách Mở rộng IMAP4

Đoạn mã dưới đây cho bạn thấy cách sử dụng lệnh danh sách mở rộng IMAP4.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
final ImapClient client = new ImapClient("imap.gmail.com", 993, "username", "password");
try {
    ImapFolderInfoCollection folderInfoCol = client.listFolders("*");
    System.out.println("Extended List Supported: " + client.getExtendedListSupported());
    for (ImapFolderInfo folderInfo : (Iterable<ImapFolderInfo>) folderInfoCol) {
        if (folderInfo.getName().equals("[Gmail]/All Mail"))
            System.out.println("Has Children: " + folderInfo.hasChildren());
        if (folderInfo.getName().equals("[Gmail]/Bin"))
            System.out.println("Bin has children? " + folderInfo.hasChildren());
        if (folderInfo.getName().equals("[Gmail]/Drafts"))
            System.out.println("Drafts has children? " + folderInfo.hasChildren());
        if (folderInfo.getName().equals("[Gmail]/Important"))
            System.out.println("Important has Children? " + folderInfo.hasChildren());
        if (folderInfo.getName().equals("[Gmail]/Sent Mail"))
            System.out.println("Sent Mail has Children? " + folderInfo.hasChildren());
        if (folderInfo.getName().equals("[Gmail]/Spam"))
            System.out.println("Spam has Children? " + folderInfo.hasChildren());
        if (folderInfo.getName().equals("[Gmail]/Starred"))
            System.out.println("Starred has Children? " + folderInfo.hasChildren());
    }
} finally {
    if (client != null)
        client.dispose();
}