/// <summary>
        /// Export the eamil messges form the specific search folder.
        /// </summary>
        private static void ExportEmailMessages(SearchFolder searchFolder, String filePath)
        {
            if (searchFolder == null)
            {
                return;
            }

            String[] invalidStings = { "\\", ",", ":", "*", "?", "\"", "<", ">", "|" };

            PropertySet itemPorpertySet = new PropertySet(BasePropertySet.FirstClassProperties,
                                                          EmailMessageSchema.MimeContent);

            const Int32 pageSize = 50;
            ItemView    itemView = new ItemView(pageSize);

            FindItemsResults <Item> findResults = null;

            do
            {
                findResults = searchFolder.FindItems(itemView);

                foreach (Item item in findResults.Items)
                {
                    if (item is EmailMessage)
                    {
                        EmailMessage email = item as EmailMessage;
                        email.Load(itemPorpertySet);

                        Byte[] content  = email.MimeContent.Content;
                        String fileName = email.Subject;

                        // Replace all the invaild strings.
                        foreach (String str in invalidStings)
                        {
                            fileName = fileName.Replace(str, "");
                        }

                        // Export the emails to the .eml files.
                        fileName = Path.Combine(filePath, fileName + ".eml");
                        File.WriteAllBytes(fileName, content);
                        Console.WriteLine("Export the email:{0}", email.Subject);
                    }
                }

                itemView.Offset += pageSize;
            } while (findResults.MoreAvailable);
        }
Exemplo n.º 2
0
        public static void SearchBySearchFolder(SearchFolder folder)
        {
            var rnd  = new Random();
            var view = new ItemView(1, 0);

            view.OrderBy.Add(MyNamedProp, SortDirection.Ascending);
            var sw = new Stopwatch();

            Console.WriteLine("SearchBySearchFolder started.");
            sw.Start();
            for (var x = 0; x < NumberOfSearchesToPerform; x++)
            {
                var numberToSearchFor = rnd.Next(0, NumberOfMessagesToCreate);
                var filter            = new SearchFilter.IsEqualTo(MyNamedProp, numberToSearchFor);
                var results           = folder.FindItems(filter, view);
            }

            sw.Stop();
            Console.WriteLine("SearchBySearchFolder stopped after: " + sw.ElapsedMilliseconds + " milliseconds.");
        }