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

Bỏ qua hoặc Vượt qua Chứng chỉ SSL Không hợp lệ hoặc Đã hết hạn

Aspose.Email kết nối tới Exchange Server qua HTTPS, vì vậy JVM xác thực chứng chỉ máy chủ trước khi bất kỳ yêu cầu EWS nào được thực hiện. Nếu chứng chỉ đã hết hạn, tự ký, hoặc chủ đề không khớp với tên máy chủ, việc kết nối sẽ thất bại với một SSLHandshakeException.

Sửa lỗi được hỗ trợ là cài đặt chứng chỉ máy chủ (hoặc CA đã phát hành) vào kho tin cậy JVM:

keytool -importcert -alias exchange -file exchange.cer -keystore "%JAVA_HOME%\lib\security\cacerts"

Đối với môi trường thử nghiệm hoặc phát triển mà không thực tế, bạn có thể đăng ký một TrustManager và bộ xác minh tên máy chủ trước khi tạo khách hàng. Gọi phương thức này một lần duy nhất khi khởi động ứng dụng:

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;

public class SslBypass {

  public static void trustAllCertificates() throws Exception {
      TrustManager[] trustAll = new TrustManager[] {
          new X509TrustManager() {
              public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
              public void checkClientTrusted(X509Certificate[] chain, String authType) { }
              public void checkServerTrusted(X509Certificate[] chain, String authType) { }
          }
      };

      SSLContext sslContext = SSLContext.getInstance("TLS");
      sslContext.init(null, trustAll, new java.security.SecureRandom());

      HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
      HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
  }
}