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:CreateFoldersOnExchangeServerMailbox // Create instance of EWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); string inbox = client.MailboxInfo.InboxUri; string folderName1 = "EMAILNET-35054"; string subFolderName0 = "2015"; string folderName2 = folderName1 + "/" + subFolderName0; string folderName3 = folderName1 + " / 2015"; ExchangeFolderInfo rootFolderInfo = null; ExchangeFolderInfo folderInfo = null; try { client.UseSlashAsFolderSeparator = true; client.CreateFolder(client.MailboxInfo.InboxUri, folderName1); client.CreateFolder(client.MailboxInfo.InboxUri, folderName2); } finally { if (client.FolderExists(inbox, folderName1, out rootFolderInfo)) { if (client.FolderExists(inbox, folderName2, out folderInfo)) { client.DeleteFolder(folderInfo.Uri, true); } client.DeleteFolder(rootFolderInfo.Uri, true); } } // ExEnd:CreateFoldersOnExchangeServerMailbox }
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: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 }
private static void ListSubFolders(ExchangeClient client, ExchangeFolderInfo folderInfo) { 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 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) { } }
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 }
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); } } }
// 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) { } }