private void ProcessRules(IMailFolder inbox) { inbox.Open(FolderAccess.ReadWrite); inbox.Status(StatusItems.Unread); if (inbox.Unread > 0) { var unreadMessageUids = inbox.Search(SearchQuery.NotSeen); var toMove = new Dictionary<string, List<UniqueId>>(); var markAsRead = new List<UniqueId>(); // process unread messages foreach (var unreadMessageUid in unreadMessageUids) { var message = inbox.GetMessage(unreadMessageUid); var matchingRule = GetMatchingRule(message); if (matchingRule != null) { if (!toMove.ContainsKey(matchingRule.Destination)) { toMove.Add(matchingRule.Destination, new List<UniqueId>()); } toMove[matchingRule.Destination].Add(unreadMessageUid); if (matchingRule.MarkAsRead) { markAsRead.Add(unreadMessageUid); } } } // mark as read if (markAsRead.Any()) { inbox.AddFlags(markAsRead, MessageFlags.Seen, true); } // move to destination if (toMove.Any()) { foreach (var destination in toMove.Keys) { inbox.MoveTo(toMove[destination], inbox.GetSubfolder(destination)); } } } }