Connessione a Exchange Server
Per connettersi ai server Exchange 2007, 2010 e 2013 usando Exchange Web Service, Aspose.Email fornisce il IEWSClient interfaccia che implementa il EWSClient classe. Il EWSClient.getEWSClient il metodo istanzia e restituisce un IEWSClient oggetto che è poi usato per eseguire operazioni relative a una casella di posta Exchange e altre cartelle. Questo articolo mostra come istanziare oggetti di IEWSClient.
Connessione a Exchange Server usando EWS
Il seguente frammento di codice mostra come connettersi 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;
}
Connessione a Exchange Server usando IMAP
Microsoft Exchange Server supporta il protocollo IMAP per accedere agli elementi di una casella di posta. Usa le funzionalità di Aspose.Email ImapClient classe per connettersi a Exchange Server usando il protocollo IMAP. Per ulteriori informazioni su ImapClient classe. Prima, assicurati che i servizi IMAP siano abilitati per il tuo Exchange Server:
- Apri il Pannello di controllo.
- Vai su Strumenti di amministrazione, poi Servizi.
- Verifica lo stato del servizio Microsoft Exchange IMAP4.
- Se non è già in esecuzione, abilitalo/avvialo.
Il seguente frammento di codice mostra come connettersi e elencare i messaggi dalla cartella Posta in arrivo del server Microsoft Exchange usando il protocollo 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();
Il seguente frammento di codice mostra come usare 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();
}
Dopo essersi connessi a un server Exchange usando IMAP e avere ottenuto il IMapMessageInfoCollection, Il seguente frammento di codice mostra come usare il MessageInfo numero di sequenza dell’oggetto per salvare un messaggio specifico.
// 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());
}