Conectándose a Exchange Server

Para conectarse a los servidores Exchange 2007, 2010 y 2013 usando Exchange Web Service, Aspose.Email proporciona el IEWSClient interfaz que implementa el EWSClient clase. El EWSClient.getEWSClient el método instancia y devuelve un IEWSClient objeto que se utiliza posteriormente para realizar operaciones relacionadas con un buzón Exchange y otras carpetas. Este artículo muestra cómo instanciar objetos de IEWSClient.

Conectándose a Exchange Server usando EWS

El siguiente fragmento de código muestra cómo conectarse usando 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;
}

Conectándose a Exchange Server usando IMAP

Microsoft Exchange Server soporta el protocolo IMAP para acceder a elementos en un buzón. Use Aspose.Email ImapClient clase para conectarse al Exchange Server usando el protocolo IMAP. Para más información sobre el ImapClient clase. Primero, asegúrese de que los servicios IMAP estén habilitados para su Exchange Server:

  1. Abra el Panel de control.
  2. Vaya a Herramientas de administrador, luego a Servicios.
  3. Verifique el estado del servicio Microsoft Exchange IMAP4.
  4. Si no está en ejecución, habilítelo/inícielo.

El siguiente fragmento de código muestra cómo conectarse y listar mensajes de la carpeta Bandeja de entrada de Microsoft Exchange Server usando el protocolo 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();

El siguiente fragmento de código muestra cómo usar 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();
}

Después de conectarse a un servidor Exchange usando IMAP y obtener el IMapMessageInfoCollection, el siguiente fragmento de código muestra cómo usar el MessageInfo número de secuencia del objeto para guardar un mensaje específico.

// 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());
}