How could I fetch multiple o365 mail details for mail ids using EWS managed API in c#

121 views Asked by At

I need to fetch multiple o365 mail details for different mail ids using EWS managed API in c#. Suppose I have o365 mail ids like 1,2,3...

When I will pass these mail ids and call EWS managed API then the details for mail ids should populated. I have done the details population for a single email id using the code like below:

 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
            service.Credentials = new WebCredentials("username", "Password");
            service.AutodiscoverUrl(Ownerusername, RedirectionUrlValidationCallback);
            EmailMessage mail = EmailMessage.Bind(service, mailID, PropertySet.FirstClassProperties);

If anybody has any suggestion please share.

1

There are 1 answers

7
Marcus Höglund On BEST ANSWER

Try this:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new WebCredentials("username", "Password");
service.AutodiscoverUrl(Ownerusername, RedirectionUrlValidationCallback);

//Make sure you include the properties you are looking for in EmailMessageSchema
PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.Subject, EmailMessageSchema.ToRecipients);

//Add your Ids stored in the database here
var itemIds = new List<ItemId>();
foreach(var MailIds in db.Mails.select(a=>a.Ids))
{
    itemIds.add(new ItemId(MailIds));
}

//Send one request to EWS and get all mails by Id
ServiceResponseCollection<GetItemResponse> response = service.BindToItems(itemIds, propSet);

//Get the emails
foreach (GetItemResponse getItemResponse in response)
{
    Item item = getItemResponse.Item;
    EmailMessage message = (EmailMessage)item;
}