اتصال به سرور Exchange

برای اتصال به سرورهای Exchange 2007، 2010 و 2013 با استفاده از Exchange Web Service، Aspose.Email فراهم می‌کند IEWSClient رابطی که پیاده‌سازی می‌کند EWSClient کلاس. EWSClient.getEWSClient متد یک IEWSClient شیئی که سپس برای انجام عملیات مرتبط با صندوق‌نامه Exchange و پوشه‌های دیگر استفاده می‌شود. این مقاله نشان می‌دهد چگونه اشیاء از IEWSClient.

اتصال به سرور Exchange با استفاده از EWS

قطعه کد زیر نشان می‌دهد چگونه با استفاده از 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;
}

اتصال به سرور Exchange با استفاده از IMAP

Microsoft Exchange Server از پروتکل IMAP برای دسترسی به آیتم‌های یک صندوق‌نامه پشتیبانی می‌کند. از Aspose.Email’s ImapClient کلاس برای اتصال به سرور Exchange با استفاده از پروتکل IMAP. برای اطلاعات بیشتر دربارهٔ ImapClient کلاس. ابتدا اطمینان حاصل کنید که سرویس‌های IMAP برای سرور Exchange شما فعال هستند:

  1. پنل کنترل را باز کنید.
  2. به ابزارهای مدیر (Administrator Tools) رفته، سپس Services.
  3. وضعیت سرویس Microsoft Exchange IMAP4 را بررسی کنید.
  4. اگر قبلاً در حال اجرا نیست، آن را فعال/شروع کنید.

قطعه کد زیر نشان می‌دهد چگونه با استفاده از پروتکل IMAP به سرور Microsoft Exchange متصل شوید و پیام‌های پوشه Inbox را فهرست کنید.

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

پس از اتصال به سرور Exchange با استفاده از IMAP و دریافت 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());
}

نادیده گرفتن یا دور زدن گواهی SSL نامعتبر یا منقضی‌شده

Aspose.Email connects to Exchange Server over HTTPS, so the JVM validates the server certificate before any EWS request is made. If the certificate has expired, is self-signed, or its subject does not match the host name, the connection fails with an SSLHandshakeException.

The supported fix is to install the server certificate (or the CA that issued it) into the JVM trust store:

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

For a test or development environment where that is not practical, you can register a permissive TrustManager and host name verifier before creating the client. Call this method once during application start-up:

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