Łą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:

  1. Otwórz Panel sterowania.
  2. Przejdź do Narzędzia administratora, a następnie Usługi.
  3. Sprawdź status usługi Microsoft Exchange IMAP4.
  4. 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());
}