ExchangeService 온라인 outlook 메일박스에서 읽지 않은 메일 가져오기

7966 단어 Exchange
using Microsoft.Exchange.WebServices.Data;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Text;

using System.Threading.Tasks;



namespace ExchangeTest

{

    class Program

    {

        static ExchangeService service;

        static string userEmail = "[email protected]";

        static string userPassword = "xxxxx";



        static void Main(string[] args)

        {

            Console.WriteLine("Connecting to Exchange Online, please wait...");

            service = new ExchangeService(ExchangeVersion.Exchange2013);

            service.Credentials = new WebCredentials(userEmail, userPassword);

            service.AutodiscoverUrl(userEmail, RedirectionUrlValidationCallback);



            // 

            int unRead = Folder.Bind(service, WellKnownFolderName.Inbox).UnreadCount;



            GetUnreadEmails();

        }



        /// <summary>

        ///  

        /// </summary>

        private static void GetUnreadEmails()

        {

            ItemView view = new ItemView(int.MaxValue);

            FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, SetFilter(), view);

            List<string> list = new List<string>();



            foreach (Item item in findResults.Items)

            {

                if (item.Subject != null)

                {

                    list.Add(item.Subject.ToString());

                }

                else

                {

                    list.Add(" ");

                }

                //list.Add(item.DateTimeSent.ToString());

            }

        }



        /// <summary>

        ///  

        /// </summary>

        private static SearchFilter SetFilter()

        {

            List<SearchFilter> searchFilterCollection = new List<SearchFilter>();

            searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));

            SearchFilter s = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection.ToArray());



            ////List<SearchFilter> searchFilterCollection = new List<SearchFilter>();

            //searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));

            //searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true));

            //SearchFilter s = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());

            

            return s;

        }



        /// <summary>

        ///  URL 

        /// </summary>

        static bool RedirectionUrlValidationCallback(String redirectionUrl)

        {

            bool redirectionValidated = false;

            if (redirectionUrl.Equals("https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml"))

                redirectionValidated = true;



            return redirectionValidated;

        }

        

    }

}

좋은 웹페이지 즐겨찾기