Свързване към Exchange Server

За да се свържете към Exchange сървъри 2007, 2010 и 2013, използвайки Exchange Web Service, Aspose.Email предоставя IEWSClient интерфейс, който имплементира EWSClient клас. Този EWSClient.getEWSClient метод създава и връща един IEWSClient обект, който се използва по-нататък за извършване на операции, свързани с Exchange пощенска кутия и други папки. Тази статия показва как да създадете обекти от IEWSClient.

Свързване към Exchange Server чрез EWS

Следният кодов откъс показва как да се свържете, използвайки 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;
}

Свързване към Exchange Server чрез IMAP

Microsoft Exchange Server поддържа протокола IMAP за достъп до елементи в пощенска кутия. Използвайте Aspose.Email’s ImapClient клас за свързване към Exchange Server, използвайки протокола IMAP. За повече информация относно ImapClient клас. Първо, уверете се, че IMAP услугите са активирани за вашия Exchange Server:

  1. Отворете Control Panel.
  2. Отидете в Administrator Tools, след което Services.
  3. Проверете състоянието на услугата Microsoft Exchange IMAP4.
  4. Ако вече не е стартиран, активирайте/стартирайте го.

Следният кодов откъс показва как да се свържете и изброите съобщения от папката Inbox на Microsoft Exchange Server, използвайки протокола 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();

Следният кодов откъс показва как да се използва 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();
}

След като се свържете към Exchange сървър, използвайки IMAP, и получите 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());
}