Conectando ao Exchange Server
Para conectar aos servidores Exchange 2007, 2010 e 2013 usando Exchange Web Service, o Aspose.Email fornece o IEWSClient interface que implementa o EWSClient classe. O EWSClient.getEWSClient método instancia e retorna um IEWSClient objeto que é posteriormente usado para executar operações relacionadas a uma caixa de correio Exchange e outras pastas. Este artigo mostra como instanciar objetos de IEWSClient.
Conectando ao Exchange Server usando EWS
O trecho de código a seguir mostra como conectar 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;
}
Conectando ao Exchange Server usando IMAP
Microsoft Exchange Server suporta o protocolo IMAP para acessar itens em uma caixa de correio. Use o Aspose.Email ImapClient classe para conectar ao Exchange Server usando o protocolo IMAP. Para mais informações sobre o ImapClient classe. Primeiro, certifique-se de que os serviços IMAP estejam habilitados para o seu Exchange Server:
- Abra o Painel de Controle.
- Vá para Ferramentas Administrativas, depois Serviços.
- Verifique o status do serviço Microsoft Exchange IMAP4.
- Se não estiver em execução, habilite/inicie-o.
O trecho de código a seguir mostra como conectar e listar mensagens da pasta Caixa de Entrada de um Microsoft Exchange Server usando o 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();
O trecho de código a seguir mostra como 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();
}
Depois de conectar a um servidor Exchange usando IMAP e obter o IMapMessageInfoCollection, O trecho de código a seguir mostra como usar o MessageInfo número de sequência do objeto para salvar uma mensagem específica.
// 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());
}