Exemplo n.º 1
0
        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.DeleteMessage(messageInfo.UniqueUri);
                    }
                    string subj1 = string.Format("NETWORKNET_33354 {0} {1}", "User", "User1");
                    client1.AppendMessage(folder, new MailMessage("*****@*****.**", "*****@*****.**", subj1, ""));

                    foreach (ExchangeMessageInfo messageInfo in client2.ListMessages(folder))
                    {
                        client2.DeleteMessage(messageInfo.UniqueUri);
                    }
                    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.DeleteMessage(messageInfo.UniqueUri);
                        }
                        foreach (ExchangeMessageInfo messageInfo in client2.ListMessages(folder))
                        {
                            client2.DeleteMessage(messageInfo.UniqueUri);
                        }
                    }
                    catch { }
                }
            }
            // ExEnd:ExchangeImpersonationUsingEWS
        }
        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.DeleteMessage(msgInfo.UniqueUri); // EWS
                    Console.WriteLine("Message deleted...." + msgInfo.Subject);
                }
                else
                {
                    // Do something else
                }
            }
            // ExEnd:DeleteMessagesFromusingEWS
        }