Delete Mail Messages in List

I am wondering if there is a way to use a For Each to go through a list of mail messages and remove some mail items based on their subject line while looping through.

I have attempted to use an Invoke Method (RemoveAt) in the loop to remove the mail message at the current index. However this does not work because it alters the list and messes up the loop.

Does anyone have a good way of doing this?


Hi @jpreziuso

Think it is not removed because the list was in use because of for each, as an alternate we can do like,

  1. Create a another list (of type integer) variable name tempList

  2. For each mail and inside it add the indexes you want to remove to the list (tempList)

  3. Now all the indexes we want to remove is on the new list.

  4. Use for each and set type argument as integer and pass the tempList

  5. Inside the for each use the invoke method and pass the argument as item.


Also we can do like instead of removing the items we don’t want we can add the items we want to other list,

  1. Create a list of type mail message (variable name newList)

  2. In for each mail, use add to collection activity and add the item you want to the new list (newList)