Exchange Server'a Bağlanma

Exchange Web Service kullanarak Exchange sunucularına 2007, 2010 ve 2013 bağlanmak için Aspose.Email şu … sağlar. IEWSClient arayüz, … uygular EWSClient sınıf. EWSClient.getEWSClient metod bir … örnekleyip döndürür IEWSClient nesne, bir Exchange posta kutusu ve diğer klasörlerle ilgili işlemleri gerçekleştirmek için daha sonra kullanılır. Bu makale, … nesnelerinin nasıl örneklenebileceğini gösterir. IEWSClient.

EWS kullanarak Exchange Server’a Bağlanma

Aşağıdaki kod parçacığı, Exchange Web Service (EWS) kullanarak nasıl bağlanacağınızı gösterir.

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 kullanarak Exchange Server’a Bağlanma

Microsoft Exchange Server, bir posta kutusundaki öğelere erişmek için IMAP protokolünü destekler. Aspose.Email’in … kullanın ImapClient IMAP protokolünü kullanarak Exchange Server’a bağlanan sınıf. Daha fazla bilgi için … ImapClient class. Öncelikle, Exchange Server’ınız için IMAP hizmetlerinin etkin olduğundan emin olun:

  1. Denetim Masası’nı açın.
  2. Yönetim Araçları’na gidin, ardından Hizmetler.
  3. Microsoft Exchange IMAP4 hizmetinin durumunu kontrol edin.
  4. Henüz çalışmıyorsa, etkinleştirin / başlatın.

Aşağıdaki kod parçacığı, IMAP protokolünü kullanarak Microsoft Exchange sunucusunun Gelen Kutusu klasöründen bağlanıp mesajları listelemeyi gösterir.

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

Aşağıdaki kod parçacığı, SSL kullanımını gösterir.

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 kullanarak bir Exchange sunucusuna bağlandıktan ve … aldığınızda IMapMessageInfoCollection, Aşağıdaki kod parçacığı, … kullanımını gösterir MessageInfo nesnenin belirli bir mesajı kaydetmek için sıra numarası.

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

Geçersiz veya Süresi Dolmuş SSL Sertifikasını Yok Say veya Atla

Aspose.Email, Exchange Server’a HTTPS üzerinden bağlanır, bu yüzden JVM herhangi bir EWS isteği yapılmadan önce sunucu sertifikasını doğrular. Sertifika süresi dolmuş, kendinden imzalı ya da konusu ana makine adıyla eşleşmiyorsa, bağlantı bir SSLHandshakeException.

Desteklenen çözüm, sunucu sertifikasını (veya onu imzalayan CA’yı) JVM güven mağazasına yüklemektir:

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

Test veya geliştirme ortamı için bu pratik değilse, izin veren bir TrustManager ve istemciyi oluştururken ana makine adı doğrulayıcısını destekler. Bu yöntemi uygulama başlangıcında bir kez çağırın:

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