Kết nối tới Exchange Server

Để kết nối tới các máy chủ Exchange 2007, 2010 và 2013 bằng Exchange Web Service, Aspose.Email cung cấp IEWSClient giao diện triển khai EWSClient lớp. EWSClient.getEWSClient phương pháp tạo và trả về một IEWSClient đối tượng được sử dụng tiếp để thực hiện các thao tác liên quan tới hộp thư Exchange và các thư mục khác. Bài viết này chỉ ra cách tạo các đối tượng của IEWSClient.

Kết nối tới Exchange Server bằng EWS

Đoạn mã dưới đây cho bạn thấy cách kết nối bằng 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;
}

Kết nối tới Exchange Server bằng IMAP

Microsoft Exchange Server hỗ trợ giao thức IMAP để truy cập các mục trong hộp thư. Sử dụng Aspose.Email’s ImapClient lớp để kết nối tới Exchange Server bằng giao thức IMAP. Để biết thêm thông tin về ImapClient lớp. Đầu tiên, hãy chắc chắn rằng dịch vụ IMAP đã được bật cho Exchange Server của bạn:

  1. Mở Bảng Điều Khiển.
  2. Đi tới Công cụ Quản trị, sau đó Dịch vụ.
  3. Kiểm tra trạng thái của dịch vụ Microsoft Exchange IMAP4.
  4. Nếu nó chưa chạy, hãy bật/bắt đầu nó.

Đoạn mã dưới đây cho bạn thấy cách kết nối và liệt kê các tin nhắn từ thư mục Inbox của Microsoft Exchange Server bằng giao thức IMAP.

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

Đoạn mã dưới đây cho bạn thấy cách sử dụng 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();
}

Sau khi kết nối tới máy chủ Exchange bằng IMAP và nhận được IMapMessageInfoCollection, Đoạn mã dưới đây cho bạn thấy cách sử dụng MessageInfo số thứ tự của đối tượng để lưu một tin nhắn cụ thể.

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