在 Java 中處理 MS Exchange Server 上的對話

通常,一封電子郵件由多條消息(或回复)組成,形成一個線程。在 Microsoft Exchange Server 中,這些線程被稱為對話。在以編程方式使用 MS Exchange Server 時,您可能需要訪問和管理對話。為此,在本文中,您將學習如何使用 Java 在 Microsoft Exchange Server 上查找、複製、移動和刪除對話。

用於處理 MS Exchange Server 上的對話的 Java API

為了處理 Microsoft Exchange Server 上的對話,我們將使用 Aspose.Email for Java。這是一個了不起的 API,它提供了一系列功能,可以在 Java 應用程序中與 MS Exchange Server 一起工作。您可以 下載 API 或使用以下 Maven 配置安裝它。

存儲庫:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>

依賴:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-email</artifactId>
    <version>22.2</version>
    <classifier>jdk16</classifier>
</dependency>

在 Java 中查找 MS Exchange Server 上的對話

以下是使用 Java 從 MS Exchange Server 中的文件夾查找對話的步驟。

以下代碼示例顯示如何使用 Java 從 MS Exchange Server 中的文件夾中查找對話。

// 設置郵箱URI、用戶名、密碼、域信息
String mailboxUri = "https://ex2010/ews/exchange.asmx";
String username = "test.exchange";
String password = "pwd";
String domain = "ex2010.local";
NetworkCredential credentials = new NetworkCredential(username, password, domain);

IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);
System.out.println("Connected to Exchange");

// 在收件箱文件夾中查找對話項目
ExchangeConversation[] conversations = client.findConversations(client.getMailboxInfo().getInboxUri());

// 顯示所有對話
for (ExchangeConversation conversation : conversations) {
	// 顯示會話屬性,如 Id 和 Topic
	System.out.println("Topic: " + conversation.getConversationTopic());
	System.out.println("Flag Status: " + conversation.getFlagStatus());
	System.out.println();
}

在 Java 中復制 MS Exchange Server 上的對話

您還可以將對話從一個文件夾複製到另一個文件夾。讓我們看看如何用 Java 將收件箱中的對話複製到 Exchange Server 的已刪除項目文件夾中。

以下代碼示例顯示瞭如何使用 Java 在 MS Exchange Server 中復制對話。

// 設置郵箱URI、用戶名、密碼、域信息
String mailboxUri = "https://ex2010/ews/exchange.asmx";
String username = "test.exchange";
String password = "pwd";
String domain = "ex2010.local";
NetworkCredential credentials = new NetworkCredential(username, password, domain);

IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);
System.out.println("Connected to Exchange");

// 在收件箱文件夾中查找對話項目
ExchangeConversation[] conversations = client.findConversations(client.getMailboxInfo().getInboxUri());

// 顯示所有對話
for (ExchangeConversation conversation : conversations) {
	System.out.println("Topic: " + conversation.getConversationTopic());
    
	// 根據某些條件複製對話項目
    if (conversation.getConversationTopic().contains("test email")) {
        client.copyConversationItems(conversation.getConversationId(), client.getMailboxInfo().getDeletedItemsUri());
        System.out.println("Copied the conversation item to another folder");
    }
}

在 Java 中移動 MS Exchange Server 上的對話

在上一節中,我們只是將對話從一個文件夾複製到另一個文件夾。但是,在某些情況下,您可能需要將對話移動到特定文件夾。以下是使用 Java 在 MS Exchange Server 中移動對話的步驟。

以下代碼示例顯示瞭如何使用 Java 在 MS Exchange Server 中移動對話。

// 設置郵箱URI、用戶名、密碼、域信息
String mailboxUri = "https://ex2010/ews/exchange.asmx";
String username = "test.exchange";
String password = "pwd";
String domain = "ex2010.local";
NetworkCredential credentials = new NetworkCredential(username, password, domain);

IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);
System.out.println("Connected to Exchange");

// 在收件箱文件夾中查找對話項目
ExchangeConversation[] conversations = client.findConversations(client.getMailboxInfo().getInboxUri());

// 顯示所有對話
for (ExchangeConversation conversation : conversations) {
	System.out.println("Topic: " + conversation.getConversationTopic());
    
	// 根據某種條件移動對話項目
    if (conversation.getConversationTopic().contains("test email") == true) {
        client.moveConversationItems(conversation.getConversationId(), client.getMailboxInfo().getDeletedItemsUri());
        System.out.println("Moved the conversation item to another folder");
    }
}

在 Java 中刪除 MS Exchange Server 上的對話

最後,讓我們看看如何用 Java 從 MS Exchange Server 中刪除對話。

以下代碼示例顯示瞭如何使用 Java 從 MS Exchange Server 中刪除對話。

// 設置郵箱URI、用戶名、密碼、域信息
String mailboxUri = "https://ex2010/ews/exchange.asmx";
String username = "test.exchange";
String password = "pwd";
String domain = "ex2010.local";
NetworkCredential credentials = new NetworkCredential(username, password, domain);

IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);
System.out.println("Connected to Exchange");

// 在收件箱文件夾中查找對話項目
ExchangeConversation[] conversations = client.findConversations(client.getMailboxInfo().getInboxUri());

// 顯示所有對話
for (ExchangeConversation conversation : conversations) {
	System.out.println("Topic: " + conversation.getConversationTopic());
    
	// 根據某種條件刪除對話項
    if (conversation.getConversationTopic().contains("test email") == true) {
        client.deleteConversationItems(conversation.getConversationId());
        System.out.println("Deleted the conversation item");
    }
}

獲取免費的 API 許可證

您可以獲得免費的臨時許可證,以在沒有評估限制的情況下使用 Aspose.Email for Java。

結論

在本文中,您了解瞭如何使用 Java 管理 Microsoft Exchange Server 中的對話。您已經了解瞭如何使用 Java 以編程方式查找、複製、移動和刪除 MS Exchange Server 上的對話。此外,您可以瀏覽 文檔 以閱讀有關 Aspose.Email for Java 的更多信息。此外,您可以通過我們的 論壇 提問。

也可以看看