private void PopulateFoldersExchangeClient(IEWSClient client, string folderUri, TreeNodeCollection nodes) { // Get subfolders of the current URI ExchangeFolderInfoCollection folderInfoCollection = client.ListSubFolders(folderUri); // Add the subfolders to the current tree node foreach (ExchangeFolderInfo folderInfo in folderInfoCollection) { if (folderInfo.DisplayName.Equals("Conversation Action Settings", StringComparison.InvariantCultureIgnoreCase) || folderInfo.DisplayName.Equals("Quick Step Settings", StringComparison.InvariantCultureIgnoreCase) || folderInfo.DisplayName.Equals("Contacts", StringComparison.InvariantCultureIgnoreCase) || folderInfo.DisplayName.Equals("News Feed", StringComparison.InvariantCultureIgnoreCase) || folderInfo.DisplayName.Equals("Notes", StringComparison.InvariantCultureIgnoreCase) || folderInfo.DisplayName.Equals("Outbox", StringComparison.InvariantCultureIgnoreCase) || folderInfo.DisplayName.Equals("Suggested Contacts", StringComparison.InvariantCultureIgnoreCase) || folderInfo.DisplayName.Equals("Sync Issues", StringComparison.InvariantCultureIgnoreCase) || folderInfo.DisplayName.Equals("Tasks", StringComparison.InvariantCultureIgnoreCase) || folderInfo.DisplayName.Equals("Calendar", StringComparison.InvariantCultureIgnoreCase) ) { // do not add any of these folder } else { TreeNode node = new TreeNode(folderInfo.DisplayName, folderInfo.Uri); nodes.Add(node); // If there are subfolders, call function recursively to add its subfolders if (folderInfo.ChildFolderCount > 0) { PopulateFoldersExchangeClient(client, folderInfo.Uri, node.ChildNodes); } } } }
// ExStart:ListFoldersFromExchangeServer public static void Run() { try { IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); Console.WriteLine("Downloading all messages from Inbox...."); ExchangeMailboxInfo mailboxInfo = client.GetMailboxInfo(); Console.WriteLine("Mailbox URI: " + mailboxInfo.MailboxUri); string rootUri = client.GetMailboxInfo().RootUri; // List all the folders from Exchange server ExchangeFolderInfoCollection folderInfoCollection = client.ListSubFolders(rootUri); foreach (ExchangeFolderInfo folderInfo in folderInfoCollection) { // Call the recursive method to read messages and get sub-folders ListSubFolders(client, folderInfo); } Console.WriteLine("All messages downloaded."); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private static void DownloadAllMessages() { try { string rootFolder = domain + "-" + username; Directory.CreateDirectory(rootFolder); string inboxFolder = rootFolder + "\\Inbox"; Directory.CreateDirectory(inboxFolder); Console.WriteLine("Downloading all messages from Inbox...."); // Create instance of IEWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", username, password, domain); ExchangeMailboxInfo mailboxInfo = client.GetMailboxInfo(); Console.WriteLine("Mailbox URI: " + mailboxInfo.MailboxUri); string rootUri = client.GetMailboxInfo().RootUri; // List all the folders from Exchange server ExchangeFolderInfoCollection folderInfoCollection = client.ListSubFolders(rootUri); foreach (ExchangeFolderInfo folderInfo in folderInfoCollection) { // Call the recursive method to read messages and get sub-folders ListMessagesInFolder(client, folderInfo, rootFolder); } Console.WriteLine("All messages downloaded."); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public static void Run() { //ExStart: GetFolderTypeInformationUsingEWS const string mailboxUri = "https://exchange/ews/exchange.asmx"; const string domain = @""; const string username = @"*****@*****.**"; const string password = @"password"; NetworkCredential credentials = new NetworkCredential(username, password, domain); // ExStart:CopyConversations IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials); ExchangeFolderInfoCollection folderInfoCol = client.ListSubFolders(client.MailboxInfo.RootUri); foreach (ExchangeFolderInfo folderInfo in folderInfoCol) { switch (folderInfo.FolderType) { case ExchangeFolderType.Appointment: // handle Appointment break; case ExchangeFolderType.Contact: // handle Contact break; case ExchangeFolderType.Task: // handle Task break; case ExchangeFolderType.Note: // handle email message break; case ExchangeFolderType.StickyNote: // handle StickyNote break; case ExchangeFolderType.Journal: // handle Journal break; default: break; } //ExEnd: GetFolderTypeInformationUsingEWS } }
private static void ListSubFolders(IEWSClient client, ExchangeFolderInfo folderInfo) { // Create the folder in disk (same name as on IMAP server) Console.WriteLine(folderInfo.DisplayName); try { // If this folder has sub-folders, call this method recursively to get messages ExchangeFolderInfoCollection folderInfoCollection = client.ListSubFolders(folderInfo.Uri); foreach (ExchangeFolderInfo subfolderInfo in folderInfoCollection) { ListSubFolders(client, subfolderInfo); } } catch (Exception) { } }
private void PopulateFoldersExchangeClient(IEWSClient client, string folderUri, TreeNodeCollection nodes) { // Get subfolders of the current URI ExchangeFolderInfoCollection folderInfoCollection = client.ListSubFolders(folderUri); // Add the subfolders to the current tree node foreach (ExchangeFolderInfo folderInfo in folderInfoCollection) { TreeNode node = new TreeNode(folderInfo.DisplayName + "(" + folderInfo.ChildFolderCount + ")", folderInfo.Uri); nodes.Add(node); // If there are subfolders, call function recursively to add its subfolders if (folderInfo.ChildFolderCount > 0) { PopulateFoldersExchangeClient(client, folderInfo.Uri, node.ChildNodes); } } }
private static void ListSubFolders(IEWSClient client, ExchangeFolderInfo folderInfo) { // Create the folder in disk (same name as on IMAP server) Console.WriteLine(folderInfo.DisplayName); try { // If this folder has sub-folders, call this method recursively to get messages ExchangeFolderInfoCollection folderInfoCollection = client.ListSubFolders(folderInfo.Uri); foreach (ExchangeFolderInfo subfolderInfo in folderInfoCollection) { ListSubFolders(client, subfolderInfo); } } catch (Exception) { } }
private static void ListMessagesFromSubFolder(ExchangeFolderInfo publicFolder, IEWSClient client) { Console.WriteLine("Folder Name: " + publicFolder.DisplayName); ExchangeMessageInfoCollection msgInfoCollection = client.ListMessagesFromPublicFolder(publicFolder); foreach (ExchangeMessageInfo messageInfo in msgInfoCollection) { MailMessage msg = client.FetchMessage(messageInfo.UniqueUri); Console.WriteLine(msg.Subject); msg.Save(dataDir + msg.Subject + ".msg", SaveOptions.DefaultMsgUnicode); } // Call this method recursively for any subfolders if (publicFolder.ChildFolderCount > 0) { ExchangeFolderInfoCollection subfolders = client.ListSubFolders(publicFolder); foreach (ExchangeFolderInfo subfolder in subfolders) { ListMessagesFromSubFolder(subfolder, client); } } }
// Recursive method to get messages from folders and sub-folders private static void ListMessagesInFolder(IEWSClient client, ExchangeFolderInfo folderInfo, string rootFolder) { // Create the folder in disk (same name as on IMAP server) string currentFolder = rootFolder + "\\" + folderInfo.DisplayName; Directory.CreateDirectory(currentFolder); // List messages ExchangeMessageInfoCollection msgInfoColl = client.ListMessages(folderInfo.Uri); Console.WriteLine("Listing messages...."); int i = 0; foreach (ExchangeMessageInfo msgInfo in msgInfoColl) { // Get subject and other properties of the message Console.WriteLine("Subject: " + msgInfo.Subject); // Get rid of characters like ? and :, which should not be included in a file name string fileName = msgInfo.Subject.Replace(":", " ").Replace("?", " "); MailMessage msg = client.FetchMessage(msgInfo.UniqueUri); msg.Save(dataDir + "\\" + fileName + "-" + i + ".msg"); i++; } Console.WriteLine("============================\n"); try { // If this folder has sub-folders, call this method recursively to get messages ExchangeFolderInfoCollection folderInfoCollection = client.ListSubFolders(folderInfo.Uri); foreach (ExchangeFolderInfo subfolderInfo in folderInfoCollection) { ListMessagesInFolder(client, subfolderInfo, currentFolder); } } catch (Exception) { } }
private static void ListMessagesFromSubFolder(ExchangeFolderInfo publicFolder, IEWSClient client) { Console.WriteLine("Folder Name: " + publicFolder.DisplayName); ExchangeMessageInfoCollection msgInfoCollection = client.ListMessagesFromPublicFolder(publicFolder); foreach (ExchangeMessageInfo messageInfo in msgInfoCollection) { MailMessage msg = client.FetchMessage(messageInfo.UniqueUri); Console.WriteLine(msg.Subject); msg.Save(dataDir + msg.Subject + ".msg", SaveOptions.DefaultMsgUnicode); } // Call this method recursively for any subfolders if (publicFolder.ChildFolderCount > 0) { ExchangeFolderInfoCollection subfolders = client.ListSubFolders(publicFolder); foreach (ExchangeFolderInfo subfolder in subfolders) { ListMessagesFromSubFolder(subfolder, client); } } }
static void Run() { // ExStart: PagingSupportForListingFolders ///<summary> /// This example shows how to retrieve folders information from Exchange Server with Paging Support /// Introduced in Aspose.Email for .NET 6.4.0 ///</summary> using (IEWSClient client = EWSClient.GetEWSClient("exchange.domain.com", "username", "password")) { int itemsPerPage = 5; ExchangeFolderInfoCollection totalFoldersCollection = client.ListSubFolders(client.MailboxInfo.RootUri); Console.WriteLine(totalFoldersCollection.Count); //////////////////// RETREIVING INFORMATION USING PAGING SUPPORT ////////////////////////////////// List <ExchangeFolderPageInfo> pages = new List <ExchangeFolderPageInfo>(); ExchangeFolderPageInfo pagedFoldersCollection = client.ListSubFoldersByPage(client.MailboxInfo.RootUri, itemsPerPage); Console.WriteLine(pagedFoldersCollection.TotalCount); pages.Add(pagedFoldersCollection); while (!pagedFoldersCollection.LastPage) { pagedFoldersCollection = client.ListSubFoldersByPage(client.MailboxInfo.RootUri, itemsPerPage, pagedFoldersCollection.PageOffset + 1); pages.Add(pagedFoldersCollection); } int retrievedFolders = 0; foreach (ExchangeFolderPageInfo pageCol in pages) { retrievedFolders += pageCol.Items.Count; } // Verify the total count of folders Console.WriteLine(retrievedFolders); } // ExEnd: PagingSupportForListingFolders }
public static void Run() { using (IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "your.username", "your.Password")) { try { // Create an appointmenta that will be added to secondary calendar folder DateTime date = DateTime.Now; DateTime startTime = new DateTime(date.Year, date.Month, date.Day, date.Hour, 0, 0); DateTime endTime = startTime.AddHours(1); string timeZone = "America/New_York"; Appointment[] listAppointments; Appointment appointment = new Appointment("Room 121", startTime, endTime, "*****@*****.**", "*****@*****.**"); appointment.SetTimeZone(timeZone); appointment.Summary = "EMAILNET-35198 - " + Guid.NewGuid().ToString(); appointment.Description = "EMAILNET-35198 Ability to add event to Secondary Calendar of Office 365"; // Verify that the new folder has been created ExchangeFolderInfoCollection calendarSubFolders = client.ListSubFolders(client.MailboxInfo.CalendarUri); string getfolderName; string setFolderName = "New Calendar"; bool alreadyExits = false; // Verify that the new folder has been created already exits or not for (int i = 0; i < calendarSubFolders.Count; i++) { getfolderName = calendarSubFolders[i].DisplayName; if (getfolderName.Equals(setFolderName)) { alreadyExits = true; } } if (alreadyExits) { Console.WriteLine("Folder Already Exists"); } else { // Create new calendar folder client.CreateFolder(client.MailboxInfo.CalendarUri, setFolderName, null, "IPF.Appointment"); Console.WriteLine(calendarSubFolders.Count); // Get the created folder URI string newCalendarFolderUri = calendarSubFolders[0].Uri; // appointment api with calendar folder uri // Create client.CreateAppointment(appointment, newCalendarFolderUri); appointment.Location = "Room 122"; // update client.UpdateAppointment(appointment, newCalendarFolderUri); // list listAppointments = client.ListAppointments(newCalendarFolderUri); // list default calendar folder listAppointments = client.ListAppointments(client.MailboxInfo.CalendarUri); // Cancel client.CancelAppointment(appointment, newCalendarFolderUri); listAppointments = client.ListAppointments(newCalendarFolderUri); // appointment api with context current calendar folder uri client.CurrentCalendarFolderUri = newCalendarFolderUri; // Create client.CreateAppointment(appointment); appointment.Location = "Room 122"; // update client.UpdateAppointment(appointment); // list listAppointments = client.ListAppointments(); // list default calendar folder listAppointments = client.ListAppointments(client.MailboxInfo.CalendarUri); // Cancel client.CancelAppointment(appointment); listAppointments = client.ListAppointments(); } } finally { } } }
// Recursive method to get messages from folders and sub-folders private static void ListMessagesInFolder(IEWSClient client, ExchangeFolderInfo folderInfo, string rootFolder) { // Create the folder in disk (same name as on IMAP server) string currentFolder = rootFolder + "\\" + folderInfo.DisplayName; Directory.CreateDirectory(currentFolder); // List messages ExchangeMessageInfoCollection msgInfoColl = client.ListMessages(folderInfo.Uri); Console.WriteLine("Listing messages...."); int i = 0; foreach (ExchangeMessageInfo msgInfo in msgInfoColl) { // Get subject and other properties of the message Console.WriteLine("Subject: " + msgInfo.Subject); // Get rid of characters like ? and :, which should not be included in a file name string fileName = msgInfo.Subject.Replace(":", " ").Replace("?", " "); MailMessage msg = client.FetchMessage(msgInfo.UniqueUri); msg.Save(dataDir + "\\" + fileName + "-" + i + ".msg"); i++; } Console.WriteLine("============================\n"); try { // If this folder has sub-folders, call this method recursively to get messages ExchangeFolderInfoCollection folderInfoCollection = client.ListSubFolders(folderInfo.Uri); foreach (ExchangeFolderInfo subfolderInfo in folderInfoCollection) { ListMessagesInFolder(client, subfolderInfo, currentFolder); } } catch (Exception) { } }