none
Retrieval of all users in Exchange online RRS feed

  • Question

  • Exchange Version: Exchange Online at Office 365
    API Name: EWS API 2.0
    IDE: Visual basic C# 2010
    Targeted features: Impersonation and retrieval of all users

    Hi there, I am currently developing a program that uses impersonation and queries the EWS API to retrieve emails and their attachments from users. I have used the C# codes from the MSDN Library to develop the program. The program is successful in retrieving the emails from specific users that are hard coded into the program.

    The code below shows the process of authenticating the admin user and the impersonation is done in the  RetrieveEmailFromUser() function.

    ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack; ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1); service.Credentials = new NetworkCredential("adminemail@", "password", "domain.onmicrosoft.com"); service.AutodiscoverUrl("adminemail", RedirectionUrlValidationCallback); List<string> emailaddrlist = new List<string>(); emailaddrlist.Add("user1@domain.onmicrosoft.com"); emailaddrlist.Add("test2@domain.onmicrosoft.com"); foreach (string item in emailaddrlist) { string content = RetrieveEmailFromUser(service, item); Console.WriteLine(content); }


            static string RetrieveEmailFromUser(ExchangeService service, string emailAddr)
            {
                service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailAddr);

                FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(50));

    As can be seen from the coding, the users that are to be impersonated have been hardcoded. I would like to know if there are any C# codes or a link to a page where it describes how I can retrieve ALL users with mailboxes from a Microsoft 365 admin account without needing to manually add users for retrieval.

    Friday, September 5, 2014 7:53 AM

Answers

All replies

  • There are a few different ways of doing that probably the best is to use the Exchange Management Shell and get-Mailbox see http://blogs.msdn.com/b/mvpawardprogram/archive/2011/08/29/mvps-for-exchange-online-calling-exchange-online-powershell-cmdlets-from-c.aspx .

    If you want to do this with just EWS one easy way of doing this is to use a Distribution group and place all the Mailboxes you want to enumerate in that DL and then just use ExpandGroup http://msdn.microsoft.com/en-us/library/office/dn629392(v=exchg.150).aspx on the distribution list. You can also use FindPeople http://msdn.microsoft.com/en-us/library/office/jj191039(v=exchg.150).aspx but it can be hard to distinguish mailboxes from other directory objects with this method (hence the DL is generally a better idea)

    Cheers
    Glen 

    • Marked as answer by Nicholas Soh Monday, September 8, 2014 7:20 AM
    Monday, September 8, 2014 7:12 AM
  • Thank you for your reply. I will try out the solutions you have provided.
    Monday, September 8, 2014 7:20 AM
  • Hi there sorry for asking again but I have successfully retrieved all the users but now I would need to filter them to lower the amount of addresses retrieved.

    For example

    owershell.Runspace = runspace;
                                //Create the command and add the parameters to retrieve all users that have mailboxes
                                powershell.AddCommand("Get-Mailbox");
                               
                                powershell.AddParameter("RecipientTypeDetails", "UserMailbox");
     
                                //Invoke the command and store the results in a PSObject collection
                                Collection<PSObject> EmailList = powershell.Invoke();
                                
    
                                //Iterate through the results and add the PrimarySMTP address for each mailbox to emailAddrlist                 
                                foreach (PSObject emailAddr in EmailList)
                                {
                                    string time = emailAddr.Properties["WhenCreated"].Value.ToString();
                                    string addr = emailAddr.Properties["PrimarySmtpAddress"].Value.ToString();
                                    EmailAddrList.Add(addr + time);
                                }

    The property ("WhenChanged") will retrieve the date time when the mailbox had its options MODIFIED but not when an email is received. Is there a parameter I can input using powershell to retrieve users who have new emails in their mailboxes from a specific? Or can I change the value of the Property from  ("WhenChanged") to something else that will give me a date time of the most recent addition of an email to the mailbox. This will then allow me to compare the date time to filter out addresses which have not received any emails.

    Thank you.



    • Edited by Nicholas Soh Monday, September 15, 2014 4:35 AM Add two lines
    Monday, September 15, 2014 4:24 AM
  • When you listing Mailboxes this way you reading Active Directory so when changed will reflect when and if that Object was changed in the directory (which could be anything from a phone number change or password change etc). There is no directory property that will reflect when an Email was last received by a Mailbox either is there anything in EWS that will reflect this.

    The only way to get that information is by access the Mailbox itself (or use the Get-MailboxFolderStatistics cmdet which does the same thing). Or you could use the MessageTrackingLogs to detect if a Mailbox has received Email in a certain time period without accessing it. Eg the Message Tracking Logs could tell you all the Mailboxes that have received mail within a certain time period.

    Cheers
    Glen

    Tuesday, September 16, 2014 2:38 AM
  • Thank you for your quick reply. I will attempt to use the solutions provided.
    Tuesday, September 16, 2014 3:39 AM