0

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.

dllhell
  • 1,987
  • 3
  • 34
  • 51
Gopal Biswas
  • 409
  • 1
  • 7
  • 34

1 Answers1

0

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;
}
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69
  • Thanks for your valuable opinion but I need to fetch mail details for some specific mail ids at one instance. I think service.BindToItems should be an option. – Gopal Biswas May 09 '16 at 12:13
  • ok, How do you get the mail ids that you know you're going to search for? Are you doing a search on EWS to get these that isn't part of the code in the example? I can update my example with a query to get the specific depending on content of mail, cc, sender etc but not on the mail ids... – Marcus Höglund May 09 '16 at 12:45
  • I have stored mail ids in my database and based on a specific criteria some of the mail id details are need to populated.I have used service.FindItems() to find and store mail ids in my database. – Gopal Biswas May 10 '16 at 06:49
  • Updated my answer @GopalBiswas – Marcus Höglund May 10 '16 at 07:13
  • No worries, Glad to help! – Marcus Höglund May 10 '16 at 10:54
  • I have a similar requirement as you mentioned earlier, I need to fetch all the mail details for a specific sender/ mail id. Could you please write the query. – Gopal Biswas May 11 '16 at 06:09
  • Read mattks accepted answer. I think the query parameter solve your issue. http://stackoverflow.com/questions/18126804/exchange-web-services-finding-emails-sent-to-a-recipient – Marcus Höglund May 11 '16 at 06:23