Unterstützung für IMAP IDLE‑Befehl und Erweiterungen

Unterstützung für IMAP‑Idle‑Befehl

Aspose.Email API ImapClient bietet die Möglichkeit, eine Verbindung zum Server zu öffnen und auf den Eingang einer E‑Mail‑Nachricht zu warten. Das verhindert wiederholtes Abfragen des Servers nach eingehenden E‑Mails. startMonitoring nimmt zwei Handler: Einen, der aufgerufen wird, wenn Nachrichten ankommen oder gelöscht werden, und den anderen, wenn die Überwachung fehlschlägt. Das folgende Code‑Snippet zeigt, wie man den IMAP‑IDLE‑Befehl nutzt.

// 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");

Unterstützung für IMAP‑Erweiterungen

Aspose.Email‑API unterstützt IMAP‑Erweiterungen. Die nachfolgenden Beispiele zeigen die derzeit von der API unterstützten Erweiterungen. Nicht jeder Server unterstützt diese Erweiterungen.

// 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();
}

Erweiterter LIST‑Befehl von IMAP4

Das folgende Code‑Snippet zeigt, wie der erweiterte LIST‑Befehl von IMAP4 verwendet wird.

// 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();
}