Pregunta

I am using Exchaneg Web Services in c# to retrieve all the emails from a mailbox on Exchange 2010.

I am putting all the information for each email in a data table that is returned to the calling function.

I also need the unique Item ID of each email so that after I am finished I can mark the email as Read on the Exchange box.

I have tried this:

    // As a best practice, limit the properties returned to only those that are required.
    PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject);

    // Bind to the existing item by using the ItemId.
    // This method call results in a GetItem call to EWS.
    ItemId itemID = Item.Bind(service, itemId, propSet);

but it won't compile, and I don't understand what is wrong, I need the Item ID so I can store it and find the same item later in order to Mark it Read

here is the main block of code:

//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(common.strInboxURL); // new Mailbox(targetEmailAddress); @"bbtest@bocuk.local"

//creates a folder object that will point to inbox fold
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);

//this will bind the mailbox you're looking for using your service instance
Microsoft.Exchange.WebServices.Data.Folder inbox = Microsoft.Exchange.WebServices.Data.Folder.Bind(service, fid);

SearchFilter.SearchFilterCollection searchFilterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.And);
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true));
searchFilterCollection.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Sender, "@bankofcyprus.co.uk"));

// add the exceptions
for (int iEx = 0; iEx < e2c.emailExceptions.Count; iEx++)
{
    searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, e2c.emailExceptions[iEx])));                
}

ItemView view = new ItemView(100);
view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
//view.PropertySet = new PropertySet(
//    BasePropertySet.IdOnly,
//    ItemSchema.Subject,
//    ItemSchema.DateTimeReceived);

// Find the first email message in the Inbox that has attachments. This results in a FindItem operation call to EWS.
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, searchFilterCollection, view);

foreach (EmailMessage email in results)
// looping through all the emails
{
    emailSenderName = email.Sender.Name;
    sEmailSubject = email.Subject;
    emailAttachmentsCount = email.Attachments.Count;
    emailDisplayTo = email.DisplayTo;
    emailHasAttachments = email.HasAttachments;

    email.Load(new PropertySet(ItemSchema.Body) { RequestedBodyType = BodyType.Text });
    sEmailBody = email.Body;

    // As a best practice, limit the properties returned to only those that are required.
    PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject);

    // Bind to the existing item by using the ItemId. 
    // This method call results in a GetItem call to EWS.
    ItemId itemID = Item.Bind(service, itemId, propSet);


    //email.get .Load(new PropertySet(ItemId): 
    email.Load(new PropertySet(ItemSchema.DateTimeReceived));

    DataRow row = emails.NewRow();

    row["displayname"] = emailDisplayTo;
    ... (cut for brevity!
    email.Load(new PropertySet(EmailMessageSchema.Attachments));

how can I get the item ID please?

¿Fue útil?

Solución

Item.Bind returns an Item, not an ItemId, so you get a compile-time error. You already have the ItemId. It's right there on the EmailMessage you're getting with each iteration unless you're referring to other ids in Exchange, such as the EntryId.

ItemId itemID = email.Id;

This is unnecessary if you want to update the items in that block of code or thereabouts, though. For that, you just need to make the changes (mark them as read), then place them into a List or other IEnumerable and use ExchangeService.UpdateItems to update the EmailMessages.

If you want to store the ItemIds for later use, you should know that the ItemId is NOT a permanent, unchanging property. It will change if the Item in question is moved to another mailbox or moved to another folder. There may be other cases that can change it, such as service pack installations/version upgrades or even the passage of enough time, although I cannot confirm those myself.

EDIT: To answer the below statement, it seems that ContainsSubstring doesn't work too well on email addresses. You can do this with a queryString:

String queryString = "from:domain.co.uk AND isread:false AND hasattachment:true";

Give that a try. My syntax could be a hair off depending on how you want to do it. Exchange Guru Glen Scales has a nice blog post that has several links to the somewhat scattered MS documentation on AQS:

http://gsexdev.blogspot.com/2010/08/using-exchange-search-and-aqs-with-ews.html

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top