ExchangeServeice获取在线outlook邮箱中的未读邮件

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;

        }

        

    }

}

 

你可能感兴趣的:(Exchange)