الاتصال بخادم 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 للوصول إلى العناصر في صندوق البريد. استخدم ImapClient فئة للاتصال بخادم Exchange باستخدام بروتوكول IMAP. لمزيد من المعلومات حول ImapClient فئة. أولاً، تأكد من تمكين خدمات IMAP لخادم Exchange الخاص بك:

  1. افتح لوحة التحكم.
  2. انتقل إلى أدوات المسؤول، ثم الخدمات.
  3. تحقق من حالة خدمة Microsoft Exchange IMAP4.
  4. إذا لم يكن قيد التشغيل بالفعل، فمكنه/ابدأ تشغيله.

يعرض مقطع الشيفرة التالي كيفية الاتصال وقائمة الرسائل من مجلد الوارد في خادم Microsoft Exchange باستخدام بروتوكول 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();

يعرض مقطع الشيفرة التالي كيفية استخدام 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 بخادم Exchange عبر HTTPS، لذا يتحقق JVM من شهادة الخادم قبل أي طلب EWS. إذا كانت الشهادة منتهية الصلاحية أو موقعة ذاتيًا أو لا يتطابق موضوعها مع اسم المضيف، فإن الاتصال يفشل بـ SSLHandshakeException.

الإصلاح المدعوم هو تثبيت شهادة الخادم (أو السلطة المُصدرة لها) في مخزن الثقة لـ JVM:

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

لبيئة اختبار أو تطوير حيث لا يكون ذلك عمليًا، يمكنك تسجيل TrustManager ومحقق اسم المضيف قبل إنشاء العميل. استدعِ هذه الطريقة مرة واحدة أثناء بدء تشغيل التطبيق:

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