Łączenie się z serwerem Exchange
Aby połączyć się z serwerami Exchange 2007, 2010 i 2013 przy użyciu Exchange Web Service, Aspose.Email zapewnia IEWSClient interfejs, który implementuje EWSClient klasa. Ta EWSClient.getEWSClient metoda tworzy i zwraca IEWSClient obiekt, który jest dalej używany do wykonywania operacji związanych ze skrzynką pocztową Exchange i innymi folderami. Ten artykuł pokazuje, jak tworzyć obiekty IEWSClient.
Łączenie się z serwerem Exchange przy użyciu EWS
Poniższy fragment kodu pokazuje, jak połączyć się przy użyciu 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;
}
Łączenie się z serwerem Exchange przy użyciu IMAP
Microsoft Exchange Server obsługuje protokół IMAP do dostępu do elementów w skrzynce pocztowej. Użyj Aspose.Email’s ImapClient klasa do łączenia się z serwerem Exchange przy użyciu protokołu IMAP. Aby uzyskać więcej informacji o ImapClient klasa. Najpierw upewnij się, że usługi IMAP są włączone w twoim serwerze Exchange:
- Otwórz Panel sterowania.
- Przejdź do Narzędzia administratora, a następnie Usługi.
- Sprawdź status usługi Microsoft Exchange IMAP4.
- Jeśli nie jest już uruchomiony, włącz/uruchom go.
Poniższy fragment kodu pokazuje, jak połączyć się i wylistować wiadomości z folderu Odebrane serwera Microsoft Exchange przy użyciu protokołu 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();
Poniższy fragment kodu pokazuje, jak używać 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();
}
Po połączeniu się z serwerem Exchange przy użyciu IMAP i uzyskaniu IMapMessageInfoCollection, Poniższy fragment kodu pokazuje, jak używać MessageInfo numer sekwencyjny obiektu, aby zapisać konkretną wiadomość.
// 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());
}
Ignoruj lub pomijaj nieprawidłowy lub wygasły certyfikat SSL
Aspose.Email łączy się z Exchange Server przez HTTPS, więc JVM weryfikuje certyfikat serwera przed każdym żądaniem EWS. Jeśli certyfikat wygasł, jest samopodpisany lub jego podmiot nie pasuje do nazwy hosta, połączenie kończy się niepowodzeniem z SSLHandshakeException.
Wspierane rozwiązanie polega na instalacji certyfikatu serwera (lub CA, które go wydało) w magazynie zaufania JVM:
keytool -importcert -alias exchange -file exchange.cer -keystore "%JAVA_HOME%\lib\security\cacerts"
W środowisku testowym lub deweloperskim, gdzie nie jest to praktyczne, możesz zarejestrować permissive TrustManager i weryfikator nazwy hosta przed utworzeniem klienta. Wywołaj tę metodę raz przy uruchamianiu aplikacji:
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);
}
}