Beispiel #1
0
        public Conversation GetConversationById(string convID)
        {
            int          pagesize = 25;
            int          offset   = 0;
            Conversation conv     = null;

            while (conv == null)
            {
                ConversationIndexedItemView view          = new ConversationIndexedItemView(pagesize, offset, OffsetBasePoint.Beginning);
                ICollection <Conversation>  conversations = _service.FindConversation(view, WellKnownFolderName.Inbox);

                if (conversations.Count == 0)
                {
                    break;
                }
                foreach (Conversation conversation in conversations)
                {
                    string id = conversation.Id;
                    if (id.Equals(convID))
                    {
                        conv = conversation;
                        break;
                    }
                }

                offset += pagesize;
            }

            return(conv);
        }
Beispiel #2
0
        /// <summary>
        /// Demonstrates how to search for conversations by using the EWS Managed API.
        /// </summary>
        /// <param name="service"></param>
        static void FindConversation(ExchangeService service)
        {
            const int pageSize = 5;
            int       offset   = 0;

            // Create the view of conversations returned in the response. This view will return the first five
            // conversations starting with an offset of 0 from the beginning of the results set.
            ConversationIndexedItemView view = new ConversationIndexedItemView(pageSize, offset, OffsetBasePoint.Beginning);

            try
            {
                // Search the Inbox for conversations and return a results set based on the specified view.
                // This is a content index search based on the Subject and Sent properties.
                // This results in a call to EWS.
                ICollection <Conversation> conversations = service.FindConversation(view,
                                                                                    WellKnownFolderName.Inbox,
                                                                                    "Subject:Exchange Sent:01/01/2013..04/22/2013");

                // Examine properties on each conversation returned in the response.
                foreach (Conversation conversation in conversations)
                {
                    Console.WriteLine("Conversation Topic: " + conversation.Topic);
                    Console.WriteLine("Last Delivered: " + conversation.LastDeliveryTime.ToString());

                    // Identify each unique recipient of items in the conversation.
                    foreach (string GlUniqRec in conversation.GlobalUniqueRecipients)
                    {
                        Console.WriteLine("Global Unique Recipient: " + GlUniqRec);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Demonstrates how to apply actions on conversations items by using the EWS Managed API.
        /// </summary>
        /// <param name="service">An ExchangeService object that has credentials and an EWS URL set on it.</param>
        static void ApplyConversationActions(ExchangeService service)
        {
            // Create the conversation view that is returned in the response. This view will return the first
            // conversation that satisfies the search criteria.
            ConversationIndexedItemView view = new ConversationIndexedItemView(1, 0, OffsetBasePoint.Beginning);

            // Create a list of categories to apply to a conversation.
            List <string> categories = new List <string>();

            categories.Add("Customer");
            categories.Add("System Integrator");

            try
            {
                // Search the Inbox for a conversation and return a results set with the specified view.
                // This results in a call to EWS.
                ICollection <Conversation> conversations = service.FindConversation(view, WellKnownFolderName.Inbox, "Subject:\"Contoso systems\"");

                foreach (Conversation conversation in conversations)
                {
                    // Apply categorization to all items in the conversation and process the request
                    // synchronously after enabling this rule and all item categorization has been applied.
                    // This results in a call to EWS. These categories are not added to the master category list.
                    conversation.EnableAlwaysCategorizeItems(categories, true);

                    // Apply an always move rule to all items in the conversation and move the items
                    // to the Drafts folder. Process the request asynchronously and return the response.
                    // immediately. This results in a call to EWS.
                    conversation.EnableAlwaysMoveItems(WellKnownFolderName.Drafts, false);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #4
0
        /// <inheritdoc/>
        public IEnumerable <MailThread> GetEmailThreads(string folderName)
        {
            this.logger.Verbose("GetEmailThreads invoked with {folderName}.", folderName);
            var folder = exchangeService.FindFolders(
                WellKnownFolderName.Inbox,
                new SearchFilter.IsEqualTo(FolderSchema.DisplayName, folderName),
                new FolderView(1)).FirstOrDefault();

            if (folder == null)
            {
                throw new InvalidOperationException(string.Format("MailFolder {0} doesn't exist.", folderName));
            }

            // Create the view of conversations returned in the response. This view will return the first five
            // conversations, starting with an offset of 0 from the beginning of the results set.
            var view = new ConversationIndexedItemView(Int32.MaxValue, 0, OffsetBasePoint.Beginning);

            // Search the Inbox for conversations and return a results set with the specified view.
            // This results in a call to EWS.
            this.watch.Restart();
            var conversations = exchangeService.FindConversation(view, folder.Id);

            this.watch.Stop();
            this.logger.Debug("Time taken to find conversations in {folderName} = {ElapsedTime}.", folderName, this.watch.Elapsed);

            // Examine properties on each conversation returned in the response.
            foreach (var conversation in conversations)
            {
                var thread = new MailThread {
                    Id = conversation.Id.ToString(), Topic = conversation.Topic,
                };
                this.logger.Debug("Created {MailThread}", thread);

                this.logger.Verbose("Finding emails contained in the thread.");
                this.watch.Restart();
                var items = GetConversationItemsSingleConversation(exchangeService, folder.Id, conversation.Id);
                this.watch.Stop();
                this.logger.Debug("Time taken to find emails in {thread} = {ElapsedTime}", thread, this.watch.Elapsed);

                this.logger.Verbose("Convert each message to mail object.");
                this.watch.Restart();
                foreach (var item in items)
                {
                    var mail = item as EmailMessage;
                    if (mail == null)
                    {
                        this.logger.Verbose("Non email item found in {folderName}: {Item}.", folderName, item);
                        continue;
                    }

                    mail.Load(new PropertySet(BasePropertySet.FirstClassProperties)
                    {
                        RequestedBodyType = BodyType.Text
                    });

                    thread.Mails.Add(new Mail
                    {
                        Id               = mail.Id.ToString(),
                        Body             = mail.Body.Text.Trim(),
                        DateTimeReceived = mail.DateTimeReceived,
                        From             = mail.Sender.Address,
                        Recipients       = string.Join(",", mail.ToRecipients.Select(to => to.Address)),
                        Subject          = mail.Subject
                    });
                }

                this.watch.Stop();
                this.logger.Debug("Time taken to load properties for emails = {ElapsedTime}", this.watch.Elapsed);
                yield return(thread);
            }
        }