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); } }
// 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); } }
public static void Run() { try { // ExStart:EnumeratMessagesWithPaginginEWS // Create instance of ExchangeWebServiceClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "UserName", "Password"); // Call ListMessages method to list messages info from Inbox ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.GetMailboxInfo().InboxUri); int itemsPerPage = 5; List <PageInfo> pages = new List <PageInfo>(); PageInfo pagedMessageInfoCol = client.ListMessagesByPage(client.MailboxInfo.InboxUri, itemsPerPage); pages.Add(pagedMessageInfoCol); while (!pagedMessageInfoCol.LastPage) { pagedMessageInfoCol = client.ListMessagesByPage(client.MailboxInfo.InboxUri, itemsPerPage); pages.Add(pagedMessageInfoCol); } pagedMessageInfoCol = client.ListMessagesByPage(client.MailboxInfo.InboxUri, itemsPerPage); while (!pagedMessageInfoCol.LastPage) { client.ListMessages(client.MailboxInfo.InboxUri); } } catch (Exception ex) { Console.Write(ex.Message); } finally { } // ExEnd:EnumeratMessagesWithPaginginEWS }
public static void Run() { // ExStart:AccessCustomFolderUsingExchangeWebServiceClient // Create instance of EWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); // Create ExchangeMailboxInfo, ExchangeMessageInfoCollection instance ExchangeMailboxInfo mailbox = client.GetMailboxInfo(); ExchangeMessageInfoCollection messages = null; ExchangeFolderInfo subfolderInfo = new ExchangeFolderInfo(); // Check if specified custom folder exisits and Get all the messages info from the target Uri client.FolderExists(mailbox.InboxUri, "TestInbox", out subfolderInfo); messages = client.FindMessages(subfolderInfo.Uri); // Parse all the messages info collection foreach (ExchangeMessageInfo info in messages) { string strMessageURI = info.UniqueUri; // now get the message details using FetchMessage() MailMessage msg = client.FetchMessage(strMessageURI); Console.WriteLine("Subject: " + msg.Subject); } // ExEnd:AccessCustomFolderUsingExchangeWebServiceClient }
public static void Run() { // ExStart:EnumeratMessagesWithPaginginEWS // Create instance of ExchangeWebServiceClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "*****@*****.**", "LoveAir1993"); // Call ListMessages method to list messages info from Inbox ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.GetMailboxInfo().InboxUri); try { int itemsPerPage = 5; List <ExchangeMessageInfoCollection> pages = new List <ExchangeMessageInfoCollection>(); ExchangeMessageInfoCollection pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage); pages.Add(pagedMessageInfoCol); while (!pagedMessageInfoCol.LastPage) { pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage, pagedMessageInfoCol.LastItemOffset.Value + 1); pages.Add(pagedMessageInfoCol); } pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage); while (!pagedMessageInfoCol.LastPage) { client.ListMessages(client.MailboxInfo.InboxUri, pagedMessageInfoCol, itemsPerPage, pagedMessageInfoCol.LastItemOffset.Value + 1); } } finally { } // ExEnd:EnumeratMessagesWithPaginginEWS }
public static void Run() { // ExStart:MoveMessageFromOneFolderToAnotherusingEWS // Create instance of IEWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); ExchangeMailboxInfo mailboxInfo = client.GetMailboxInfo(); // List all messages from Inbox folder Console.WriteLine("Listing all messages from Inbox...."); ExchangeMessageInfoCollection msgInfoColl = client.ListMessages(mailboxInfo.InboxUri); foreach (ExchangeMessageInfo msgInfo in msgInfoColl) { // Move message to "Processed" folder, after processing certain messages based on some criteria if (msgInfo.Subject != null && msgInfo.Subject.ToLower().Contains("process this message") == true) { client.MoveItem(mailboxInfo.DeletedItemsUri, msgInfo.UniqueUri); // EWS Console.WriteLine("Message moved...." + msgInfo.Subject); } else { // Do something else } } // ExEnd:MoveMessageFromOneFolderToAnotherusingEWS }
public static void Run() { // ExStart:DeleteMessagesFromusingEWS // Create instance of IEWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); ExchangeMailboxInfo mailboxInfo = client.GetMailboxInfo(); // List all messages from Inbox folder Console.WriteLine("Listing all messages from Inbox...."); ExchangeMessageInfoCollection msgInfoColl = client.ListMessages(mailboxInfo.InboxUri); foreach (ExchangeMessageInfo msgInfo in msgInfoColl) { // Delete message based on some criteria if (msgInfo.Subject != null && msgInfo.Subject.ToLower().Contains("delete") == true) { client.DeleteItem(msgInfo.UniqueUri, DeletionOptions.DeletePermanently); // EWS Console.WriteLine("Message deleted...." + msgInfo.Subject); } else { // Do something else } } // ExEnd:DeleteMessagesFromusingEWS }
public static void Run() { // ExStart:RetrieveFolderPermissionsUsingExchangeWebServiceClient string folderName = "DesiredFolderName"; // Create instance of EWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); ExchangeFolderInfoCollection folders = client.ListPublicFolders(); ExchangeFolderPermissionCollection permissions = new ExchangeFolderPermissionCollection(); ExchangeFolderInfo publicFolder = null; try { foreach (ExchangeFolderInfo folderInfo in folders) { if (folderInfo.DisplayName.Equals(folderName)) { publicFolder = folderInfo; } } if (publicFolder == null) { Console.WriteLine("public folder was not created in the root public folder"); } ExchangePermissionCollection folderPermissionCol = client.GetFolderPermissions(publicFolder.Uri); foreach (ExchangeBasePermission perm in folderPermissionCol) { ExchangeFolderPermission permission = perm as ExchangeFolderPermission; if (permission == null) { Console.WriteLine("Permission is null."); } else { Console.WriteLine("User's primary smtp address: {0}", permission.UserInfo.PrimarySmtpAddress); Console.WriteLine("User can create Items: {0}", permission.CanCreateItems.ToString()); Console.WriteLine("User can delete Items: {0}", permission.DeleteItems.ToString()); Console.WriteLine("Is Folder Visible: {0}", permission.IsFolderVisible.ToString()); Console.WriteLine("Is User owner of this folder: {0}", permission.IsFolderOwner.ToString()); Console.WriteLine("User can read items: {0}", permission.ReadItems.ToString()); } } ExchangeMailboxInfo mailboxInfo = client.GetMailboxInfo(); //Get the Permissions for the Contacts and Calendar Folder ExchangePermissionCollection contactsPermissionCol = client.GetFolderPermissions(mailboxInfo.ContactsUri); ExchangePermissionCollection calendarPermissionCol = client.GetFolderPermissions(mailboxInfo.CalendarUri); } finally { //Do the needfull } // ExEnd:RetrieveFolderPermissionsUsingExchangeWebServiceClient }
public static void Run() { // ExStart:AccessAnotherMailboxUsingExchangeWebServiceClient // Create instance of EWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); // Get Exchange mailbox info of other email account ExchangeMailboxInfo mailboxInfo = client.GetMailboxInfo("*****@*****.**"); // ExEnd:AccessAnotherMailboxUsingExchangeWebServiceClient }
public static void Run() { // The path to the File directory. // ExStart:GetMailboxInformationFromExchangeWebServices // Create instance of EWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); // Get mailbox size, exchange mailbox info, Mailbox and Inbox folder URI Console.WriteLine("Mailbox size: " + client.GetMailboxSize() + " bytes"); ExchangeMailboxInfo mailboxInfo = client.GetMailboxInfo(); Console.WriteLine("Mailbox URI: " + mailboxInfo.MailboxUri); Console.WriteLine("Inbox folder URI: " + mailboxInfo.InboxUri); Console.WriteLine("Sent Items URI: " + mailboxInfo.SentItemsUri); Console.WriteLine("Drafts folder URI: " + mailboxInfo.DraftsUri); // ExEnd:GetMailboxInformationFromExchangeWebServices }
public static void Run() { // ExStart:ExchangeFoldersBackupToPST string dataDir = RunExamples.GetDataDir_Exchange(); // Create instance of IEWSClient class by providing credentials const string mailboxUri = "https://ews.domain.com/ews/Exchange.asmx"; const string domain = @""; const string username = @"username"; const string password = @"password"; NetworkCredential credential = new NetworkCredential(username, password, domain); IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credential); // Get Exchange mailbox info of other email account ExchangeMailboxInfo mailboxInfo = client.GetMailboxInfo(); ExchangeFolderInfo info = client.GetFolderInfo(mailboxInfo.InboxUri); ExchangeFolderInfoCollection fc = new ExchangeFolderInfoCollection(); fc.Add(info); client.Backup(fc, dataDir + "Backup_out.pst", Aspose.Email.Outlook.Pst.BackupOptions.None); // ExEnd:ExchangeFoldersBackupToPST }