Exchange Server への接続
Exchange Web Service を使用して Exchange Server 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 サービスが有効になっていることを確認してください:
- コントロール パネルを開きます。
- 管理ツールに移動し、次にサービスを開きます。
- Microsoft Exchange IMAP4 サービスのステータスを確認してください。
- まだ実行されていない場合は、有効に/開始してください。
次のコードスニペットは、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);
}
}