连接到 Exchange Server

为了使用 Exchange Web Service 连接 Exchange 2007、2010 和 2013 服务器,Aspose.Email 提供了 IEWSClient 实现了的接口 EWSClient 类。该 EWSClient.getEWSClient 方法实例化并返回一个 IEWSClient 对象随后用于执行与 Exchange 邮箱及其他文件夹相关的操作。本文展示了如何实例化以下对象的 IEWSClient.

使用 EWS 连接到 Exchange Server

以下代码片段展示了如何使用 Exchange Web Service (EWS) 进行连接。

private static IEWSClient getExchangeEWSClient() {
    final String mailboxUri = "https://outlook.office365.com/ews/exchange.asmx";
    final String domain = "";
    final String username = "username@onmicrosoft.com";
    final String password = "password";
    NetworkCredential credentials = new NetworkCredential(username, password, domain);
    IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);
    return client;
}

使用 IMAP 连接到 Exchange Server

Microsoft Exchange Server 支持 IMAP 协议以访问邮箱中的项目。使用 Aspose.Email 的 ImapClient 用于使用 IMAP 协议连接 Exchange Server 的类。有关更多信息,请参考 ImapClient 类。首先,确保已为您的 Exchange Server 启用 IMAP 服务:

  1. 打开控制面板。
  2. 转到管理工具,然后选择服务。
  3. 检查 Microsoft Exchange IMAP4 服务的状态。
  4. 如果尚未运行,请启用/启动它。

以下代码片段展示了如何使用 IMAP 协议连接并列出 Microsoft Exchange 服务器收件箱文件夹中的邮件。

// Connect to Exchange Server using ImapClient class
ImapClient imapClient = new ImapClient("ex07sp1", "Administrator", "Evaluation1");
imapClient.setSecurityOptions(SecurityOptions.Auto);

// Select the Inbox folder
imapClient.selectFolder(ImapFolderInfo.IN_BOX);

// Get the list of messages
ImapMessageInfoCollection msgCollection = imapClient.listMessages();
for (ImapMessageInfo msgInfo : (Iterable<ImapMessageInfo>) msgCollection) {
    System.out.println(msgInfo.getSubject());
}
// Disconnect from the server
imapClient.dispose();

以下代码片段展示了如何使用 SSL。

public static void run() {
    // Connect to Exchange Server using ImapClient class
    ImapClient imapClient = new ImapClient("ex07sp1", 993, "Administrator", "Evaluation1");
    imapClient.setSecurityOptions(SecurityOptions.SSLExplicit);

    // Select the Inbox folder
    imapClient.selectFolder(ImapFolderInfo.IN_BOX);

    // Get the list of messages
    ImapMessageInfoCollection msgCollection = imapClient.listMessages();
    for (ImapMessageInfo msgInfo : (Iterable<ImapMessageInfo>) msgCollection) {
        System.out.println(msgInfo.getSubject());
    }
    // Disconnect from the server
    imapClient.dispose();
}

在使用 IMAP 连接到 Exchange 服务器并获取 IMapMessageInfoCollection,以下代码片段展示了如何使用 MessageInfo 对象的序列号以保存特定消息。

// Select the Inbox folder
imapClient.selectFolder(ImapFolderInfo.IN_BOX);
// Get the list of messages
ImapMessageInfoCollection msgCollection = imapClient.listMessages();
for (ImapMessageInfo msgInfo : (Iterable<ImapMessageInfo>) msgCollection) {
    // Fetch the message from inbox using its SequenceNumber from msgInfo
    MailMessage message = imapClient.fetchMessage(msgInfo.getSequenceNumber());

    // Save the message to disc now
    message.save(dataDir + msgInfo.getSequenceNumber() + "_out.msg", SaveOptions.getDefaultMsgUnicode());
}