Exchange Server에 연결

Exchange Web Service를 사용하여 Exchange 서버 2007, 2010 및 2013에 연결하려면 Aspose.Email이 제공합니다. IEWSClient 다음 인터페이스를 구현합니다. EWSClient 클래스. EWSClient.getEWSClient 메서드는 다음을 인스턴스화하고 반환합니다. IEWSClient Exchange 메일함 및 기타 폴더와 관련된 작업을 수행하는 데 사용되는 객체입니다. 이 문서는 다음 객체를 인스턴스화하는 방법을 보여줍니다. IEWSClient.

EWS를 사용하여 Exchange Server에 연결

다음 코드 스니펫은 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;
}

IMAP을 사용하여 Exchange Server에 연결

Microsoft Exchange Server는 메일함 항목에 액세스하기 위해 IMAP 프로토콜을 지원합니다. Aspose.Email을 사용하십시오. ImapClient IMAP 프로토콜을 사용하여 Exchange Server에 연결하는 클래스입니다. 자세한 내용은 ImapClient 클래스. 먼저 Exchange Server에서 IMAP 서비스가 활성화되어 있는지 확인하십시오:

  1. 제어판을 엽니다.
  2. 관리자 도구로 이동한 다음 서비스로 이동합니다.
  3. Microsoft Exchange IMAP4 서비스 상태를 확인합니다.
  4. 이미 실행 중이 아니라면 활성화/시작하십시오.

다음 코드 스니펫은 IMAP 프로토콜을 사용하여 Microsoft Exchange Server의 받은 편지함 폴더에 연결하고 메시지를 나열하는 방법을 보여줍니다.

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

IMAP을 사용하여 Exchange 서버에 연결하고 다음을 가져온 후 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은 HTTPS를 통해 Exchange Server에 연결하므로 JVM은 모든 EWS 요청 전에 서버 인증서를 검증합니다. 인증서가 만료되었거나 자체 서명되었거나 주제가 호스트 이름과 일치하지 않으면 연결이 오류와 함께 실패합니다. SSLHandshakeException.

지원되는 해결 방법은 서버 인증서를 (또는 해당 인증서를 발급한 CA를) 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);
  }
}