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.