public static void Run() { // ExStart:ExchangeImpersonationUsingEWS // Create instance's of EWSClient class by giving credentials IEWSClient client1 = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser1", "pwd", "domain"); IEWSClient client2 = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser2", "pwd", "domain"); { string folder = "Drafts"; try { foreach (ExchangeMessageInfo messageInfo in client1.ListMessages(folder)) { client1.DeleteItem(messageInfo.UniqueUri, DeletionOptions.DeletePermanently); } string subj1 = string.Format("NETWORKNET_33354 {0} {1}", "User", "User1"); client1.AppendMessage(folder, new MailMessage("*****@*****.**", "*****@*****.**", subj1, "")); foreach (ExchangeMessageInfo messageInfo in client2.ListMessages(folder)) { client2.DeleteItem(messageInfo.UniqueUri, DeletionOptions.DeletePermanently); } string subj2 = string.Format("NETWORKNET_33354 {0} {1}", "User", "User2"); client2.AppendMessage(folder, new MailMessage("*****@*****.**", "*****@*****.**", subj2, "")); ExchangeMessageInfoCollection messInfoColl = client1.ListMessages(folder); //Assert.AreEqual(1, messInfoColl.Count); //Assert.AreEqual(subj1, messInfoColl[0].Subject); client1.ImpersonateUser(ItemChoice.PrimarySmtpAddress, "*****@*****.**"); ExchangeMessageInfoCollection messInfoColl1 = client1.ListMessages(folder); //Assert.AreEqual(1, messInfoColl1.Count); //Assert.AreEqual(subj2, messInfoColl1[0].Subject); client1.ResetImpersonation(); ExchangeMessageInfoCollection messInfoColl2 = client1.ListMessages(folder); //Assert.AreEqual(1, messInfoColl2.Count); //Assert.AreEqual(subj1, messInfoColl2[0].Subject); } finally { try { foreach (ExchangeMessageInfo messageInfo in client1.ListMessages(folder)) { client1.DeleteItem(messageInfo.UniqueUri, DeletionOptions.DeletePermanently); } foreach (ExchangeMessageInfo messageInfo in client2.ListMessages(folder)) { client2.DeleteItem(messageInfo.UniqueUri, DeletionOptions.DeletePermanently); } } catch { } } } // ExEnd:ExchangeImpersonationUsingEWS }
public static void Run() { try { // ExStart:DeleteContactsFromExchangeServerUsingEWS // Create instance of EWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); string strContactToDelete = "John Teddy"; Contact[] contacts = client.GetContacts(client.MailboxInfo.ContactsUri); foreach (Contact contact in contacts) { if (contact.DisplayName.Equals(strContactToDelete)) { client.DeleteItem(contact.Id.EWSId, DeletionOptions.DeletePermanently); } } client.Dispose(); // ExEnd:DeleteContactsFromExchangeServerUsingEWS } catch (Exception ex) { Console.WriteLine(ex.Message); } }
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:DeleteTaskOnExchange // Create instance of ExchangeClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); // Get all tasks info collection from exchange ExchangeMessageInfoCollection tasks = client.ListMessages(client.MailboxInfo.TasksUri); // Parse all the tasks info in the list foreach (ExchangeMessageInfo info in tasks) { // Fetch task from exchange using current task info ExchangeTask task = client.FetchTask(info.UniqueUri); // Check if the current task fulfills the search criteria if (task.Subject.Equals("test")) { //Delete task from exchange client.DeleteItem(task.UniqueUri, DeletionOptions.DeletePermanently); } } // ExEnd:DeleteTaskOnExchange }