mahesh kumar B M

mahesh kumar B M

  • NA
  • 44
  • 1.1m

error while reading email from EWS exchange web service c#

Mar 6 2015 2:00 PM

I have a task where i need to check the emails delivered to my mailbox and read them ,based on subject i have to do some task. But for demo purpose I have put only the basic functionality of just updating the email read status

The basic connection and creating service object everything is fine:

///////////
NetworkCredential credentials = new NetworkCredential(securelyStoredEmail, securelyStoredPassword);

ExchangeService _service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
_service.Credentials = credentials;

_service.AutodiscoverUrl("[email protected]");

/////////////////////////
Here Everything works fine. However I will invoke the below method for every 60s using observable event of reactive linq. THis is to go and poll the my emailbox and read 100 emails for every 60 seconds.

Everything works fine till sometime. Sometimes when the control reaches the line of code inside parallel.foreach loop, it shows error message like 'server cannot process this request now. Please try later' something like this. THis error comes exactly at the line

'

var email = EmailMessage.Bind(_service, findItemsResult.Id, emailProps);

'
so for every 60 seconds, i will get this error sometimes.sometimes it works fine.

Below is the method which is executed for every 60seconds. Its like i try to read the emails from "myaccount.com" for every 60s and i vil get the error 'server cannot process'.

internal void GetEmailsFrommymailbox()
{


try
{

var view = new ItemView(100);
var userMailbox = new Mailbox(userMailbox);
var folderId = new FolderId(WellKnownFolderName.Inbox, userMailbox);
SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));



var findResults = _service.FindItems(folderId, sf, view);
var emailProps = new PropertySet(ItemSchema.MimeContent, ItemSchema.Body,
ItemSchema.InternetMessageHeaders);

Parallel.ForEach(findResults, findItemsResult =>
{


///////////// this is the line where i get error////////
var email = EmailMessage.Bind(_service, findItemsResult.Id, emailProps);

//// above is the place where i get error

var emailMatching = email;
try
{
email.IsRead = true;
email.Update(ConflictResolutionMode.AutoResolve);

}
catch (Exception emailreadFromBccException)
{
Logger.Warn(emailreadFromBccException + " Unable to update email read status");
}


});
}

}