连接到 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 服务:
- 打开控制面板。
- 转到管理工具,然后选择服务。
- 检查 Microsoft Exchange IMAP4 服务的状态。
- 如果尚未运行,请启用/启动它。
以下代码片段展示了如何使用 IMAP 协议连接并列出 Microsoft Exchange 服务器收件箱文件夹中的邮件。
// 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 和主机名验证器连接到 GCC High 端点,在创建客户端之前调用此方法。请在应用程序启动时调用一次:
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);
}
}